当我点击一个按钮时,我希望能够跳转或滚动到页面上的特定div或目标。jQuery跳转或滚动到页面上的某个位置,div或目标从onclick
$('#clickMe').click(function() {
//jump to certain position or div or #target on the page
});
我该怎么做?
当我点击一个按钮时,我希望能够跳转或滚动到页面上的特定div或目标。jQuery跳转或滚动到页面上的某个位置,div或目标从onclick
$('#clickMe').click(function() {
//jump to certain position or div or #target on the page
});
我该怎么做?
$("html, body").scrollTop($(element).offset().top); // <-- Also integer can be used
我想要一个链接看起来像一个按钮,因为这样就有一个没有js的后备。
所以这就是你如何使用jquery动画跳转。 No-js后备是没有动画的正常跳跃。
原始例如:
$(document).ready(function() {
$(".jumper").on("click", function(e) {
e.preventDefault();
$("body, html").animate({
scrollTop: $($(this).attr('href')).offset().top
}, 600);
});
});
#long {
height: 500px;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Links that trigger the jumping -->
<a class="jumper" href="#pliip">Pliip</a>
<a class="jumper" href="#ploop">Ploop</a>
<div id="long">...</div>
<!-- Landing elements -->
<div id="pliip">pliip</div>
<div id="ploop">ploop</div>
氖例如与链接的实际按钮样式,只是为了证明一个观点。
除了我将类.jumper
更改为.button
并且我添加了CSS样式以使链接看起来像按钮之外,所有内容基本相同。
OP提到他们想使用一个按钮。 – RobW 2014-03-17 18:56:52
不能得到那个工作,而身体是我的DIV #goHere规定,我需要向下滚动页面到一个特定的DIV。欢呼声 – John 2013-03-01 13:35:11
嘿,你必须用'$(“#goHere”)替换'$(element)'。 '$(“body”)'将保持原样,因为我们想移动body的滚动条。 – 2013-03-01 13:36:38
这段代码在chrome中效果很好,但是在firefox中不行,至少不是在ff 21.0中:( – inadcod 2013-05-28 08:15:45