去页面加载取决于点击链接的锚点
问题描述:
嗨我有一个代码,需要我在页面加载锚定。去页面加载取决于点击链接的锚点
如果我有菜单链接是不同的锚点怎么办?当我点击link1时,它会加载一个页面并将我带到anchor1,当我点击link2时,它会将我带到同一页面,但是到达anchor2?
$(function(){
$('html, body').animate({
scrollTop: $('.anchor1').offset().top
}, 1000);
return false;
});
答
这看起来像从包含菜单链接的页面传递值到包含使用javascript的锚点的其他页面。
有很多方法可以做到这一点,其中之一是使用cookie。 让说你有下拉菜单页面,从中您在Menupage JS
$(#menuselect).change(function(){
//Store value of anchor class to passed to next page from option sel;ected in menu dropdow in cookie
$.cookie('anchorclass',$(this).val());
$(window).load('AnchorPage.htm'); // Go to page containing anchors
});
在锚页面通过锚的价值现在
<select id="menuselect">
<option value=".anchor1"> Anchor1 </option>
<option value=".anchor2"> Anchor2 </option>
<option value=".anchor3"> Anchor3 </option>
</select>
现在,您可以检查值存储在cookie并使用您的代码在页面加载时到达该锚点。
AnchorPage.htm
$(document).ready(function(){
var classofAnchor = '.anchor1'; //Set default target anchor class
if($.cookie('anchorclass') ! = null)
{
classofAnchor = $.cookie('anchorclass'); // Fetch value from cookie
}
$('html, body').animate({
scrollTop: $(classofAnchor).offset().top
}, 1000);
});
根据链接提出条件 – Ish