I am using this snippet to fade content into a div when a specific link is clicked....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
Is there a way to load the specified content using a URL instead of clicking? So I can link to www.mydomain/mypage.php#section2 or something similar?
UPDATE
Here is a jsfiddle of the simplified code /
I am using this snippet to fade content into a div when a specific link is clicked....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
Is there a way to load the specified content using a URL instead of clicking? So I can link to www.mydomain./mypage.php#section2 or something similar?
UPDATE
Here is a jsfiddle of the simplified code http://jsfiddle/k6RhR/
Share Improve this question edited Mar 7, 2014 at 15:43 fightstarr20 asked Mar 6, 2014 at 12:10 fightstarr20fightstarr20 12.7k45 gold badges171 silver badges301 bronze badges 4-
What will be the values of
link
? – Satpal Commented Mar 6, 2014 at 12:14 -
You mean when people open the URL with the # already attached? It looks like you're already doing that by doing
el.click()
. I.e. Faking a user click. – Gerben Jacobs Commented Mar 6, 2014 at 12:14 -
Are you looking for this
onhashchange
? – Rahil Wazir Commented Mar 6, 2014 at 12:16 -
Have you tried
window.location.href = link
? – Pavlo Commented Mar 6, 2014 at 12:29
3 Answers
Reset to default 3You can do something like this:
if(window.location.hash && window.location.hash=="#section2") {
// Do stuff that are meant to happen when this hash is present.
}
try This
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>mouseover demo</title>
<script src="http://code.jquery./jquery-2.1.0.min.js"></script>
<style>
.invisible
{
display: none;
}
.lorem
{
height: 300px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
getContent();
});
$(window).bind('hashchange', function (e) {
getContent();
});
function getContent() {
var url = window.location.toString();
var hash = url.substring(url.indexOf('#'));
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 2000);
$(hash).fadeIn();
$(hash).siblings("div").fadeOut();
}
</script>
</head>
<body>
<div class="lorem">
<div id="section1" class="invisible">
Content 1
<p style="text-align: justify;">
Content 1 content</p>
</div>
<div id="section2" class="invisible">
Content 2
<p style="text-align: justify;">
Content 2 content</p>
</div>
<div id="section3" class="invisible">
Content 3
<p style="text-align: justify;">
Content 3 content</p>
</div>
<div id="section4" class="invisible">
Content 4
<p style="text-align: justify;">
Content 4 content
</p>
</div>
</div>
<div id="links">
<a href="#section1" rel="section1" id="section1">Section 1</a>
<br />
<a href="#section2" rel="section2" id="section2">Section 2</a>
<br />
<a href="#section3" rel="section3" id="section3">Section 3</a>
<br />
<a href="#section4" rel="section4" id="section3">Section 4</a>
</div>
</body>
</html>
Is this what you are looking for. On load the page will look for the #section to show the content. On click of the anchor the hash link will be changed and the hash change function will load in the required content.
If you have any query please let me know.
jQuery load() allows you to specify the portion of the page like:
$( "#container" ).load( "/mypage.php #section2" );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745542545a4632193.html
评论列表(0条)