思路:
1.利用jq捕获到元素的点击事件
2.在点击函数里面添加对象为html,body的animate事件
3.让jq捕获到的元素点击的那个,把自身的name属性设置成和对应目标元素id相同,然后传上去
4.利用传上去的高仿id元素,捕获到真id元素的页面高度,从而获得移动的效果
$("html, body").animate(
{
scrollTop:$( $(this).attr("name") ).offset().top + "px"}
},
{
duration: 500,
easing: "swing"
}
)完整代码:
按钮点击部分:
<div class="leftnav1">
<a href="javascript:;" name="#jkbx">
<p>健康</p>
<p>保险</p>
</a>
</div>
<div class="leftnav2">
<a href="javascript:;" name="#ywbx">
<p>意外</p>
<p>保险</p>
</a>
</div>要跳转的位置:
<div id="jkbx"></div> <div id="ywbx"></div>
js的完整操作
$(".leftnav1 a").click(function () {
//点击完毕以后,给html,body设置animate方法
$("html, body").animate(
//变动的样式
{scrollTop: $($(this).attr("name")).offset().top + "px"},
{
//变动的时间,和速度函数
duration: 500,
easing: "swing"
}
);
});
$(".leftnav2 a").click(function () {
$("html, body").animate(
{scrollTop: $( $(this).attr("name") ).offset().top + "px"},
{
duration: 500,
easing: "swing"
}
);
})
京公网安备 11010502036488号