使用 jQuery 可以将页面滚动到特定位置吗?
我想要滚动到的位置必须具备什么条件吗?
<a name="#123">here</a>
或者它只能移动到特定的DOM id吗?
使用 jQuery 可以将页面滚动到特定位置吗?
我想要滚动到的位置必须具备什么条件吗?
<a name="#123">here</a>
或者它只能移动到特定的DOM id吗?
jQuery滚动插件
由于这是一个带有jquery标签的问题,我必须说,这个库有一个非常好用的平滑滚动插件,你可以在这里找到它: http://plugins.jquery.com/scrollTo/
来自文档的摘录:
$('div.pane').scrollTo(...);//all divs w/class pane
或者$.scrollTo(...);//the plugin will take care of this
自定义jQuery滚动函数
你可以通过定义自己的自定义滚动jQuery函数,使用非常轻量级的方法。
$.fn.scrollView = function () {
return this.each(function () {
$('html, body').animate({
scrollTop: $(this).offset().top
}, 1000);
});
}
并像这样使用:
$('#your-div').scrollView();
滚动到页面坐标
使用 scrollTop
或 scrollLeft
属性使 html
和 body
元素动画滚动
$('html, body').animate({
scrollTop: 0,
scrollLeft: 300
}, 1000);
纯JavaScript
使用window.scroll
进行滚动
window.scroll(horizontalOffset, verticalOffset);
总之,使用window.location.hash可以跳转到具有ID的元素
window.location.hash = '#your-page-element';
直接在HTML中实现(可访问性增强)
<a href="#your-page-element">Jump to ID</a>
<div id="your-page-element">
will jump here
</div>
没错,即使在纯JavaScript中也很容易。你给一个元素指定一个id,然后就可以将其作为"书签"使用:
是的,在普通的JavaScript中,这非常简单。你给一个元素指定一个ID,然后就可以将它作为"书签"使用:
<div id="here">here</div>
如果你希望当用户点击链接时它可以自动滚动到那里,你可以使用经过验证的传统方法:
<a href="#here">scroll to over there</a>
如果要以编程方式完成此操作,请使用scrollIntoView()
document.getElementById("here").scrollIntoView()
无需使用任何插件,您可以像这样操作:
var divPosition = $('#divId').offset();
然后使用此代码将文档滚动到指定的DOM:
$('html, body').animate({scrollTop: divPosition.top}, "slow");
.animate
调用发生之前重新计算。 - mikermcneil这是一个纯 JavaScript 版本:
location.hash = '#123';
它将自动滚动。 记得添加 "#" 前缀。
纯JavaScript:
window.location = "#elementId"
<div id="idVal">
<!--div content goes here-->
</div>
...
<script type="text/javascript">
$(document).ready(function(){
var divLoc = $('#idVal').offset();
$('html, body').animate({scrollTop: divLoc.top}, "slow");
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".scroll-element").click(function(){
$('html,body').animate({
scrollTop: $('.our_companies').offset().top
}, 1000);
return false;
});
})
</script>
试试这个
<div id="divRegister"></div>
$(document).ready(function() {
location.hash = "divRegister";
});
$.fn.scrollToView = function () {
return $.each(this, function () {
if ($(this).position().top < 0 ||
$(this).position().top + $(this).height() > $(this).parent().height()) {
$(this).parent().animate({
scrollTop: $(this).parent().scrollTop() + $(this).position().top
}, {
duration: 300,
queue: false
});
}
});
};
<a class="page-scroll" href="#features">Features</a>
<section id="features" class="features-section">Features Section</section>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<!-- Scrolling Nav JavaScript -->
<script src="js/jquery.easing.min.js"></script>
Jquery
//jQuery to collapse the navbar on scroll, you can use this code with in external file with name scrolling-nav.js
$(window).scroll(function () {
if ($(".navbar").offset().top > 50) {
$(".navbar-fixed-top").addClass("top-nav-collapse");
} else {
$(".navbar-fixed-top").removeClass("top-nav-collapse");
}
});
//jQuery for page scrolling feature - requires jQuery Easing plugin
$(function () {
$('a.page-scroll').bind('click', function (event) {
var anchor = $(this);
if ($(anchor).length > 0) {
var href = $(anchor).attr('href');
if ($(href.substring(href.indexOf('#'))).length > 0) {
$('html, body').stop().animate({
scrollTop: $(href.substring(href.indexOf('#'))).offset().top
}, 1500, 'easeInOutExpo');
}
else {
window.location = href;
}
}
event.preventDefault();
});
});
location.hash = 'idOfElement';
就可以了。 - Kellen Stuart