Javascript 平滑滚动到div id jQuery
我一直在尝试滚动到div id jquery代码,以使其正常工作。基于另一个堆栈溢出问题,我尝试了以下方法 演示 但它不起作用。它正好和div卡在一起。我也试过了Javascript 平滑滚动到div id jQuery,javascript,jquery,scroll,Javascript,Jquery,Scroll,我一直在尝试滚动到div id jquery代码,以使其正常工作。基于另一个堆栈溢出问题,我尝试了以下方法 演示 但它不起作用。它正好和div卡在一起。我也试过了 $('#myButton').click(function(event) { event.preventDefault(); $.scrollTo($('#myDiv'), 1000); }); 没有任何进展。您需要为html,body 演示 您确定正在加载jQuery scrollTo插件文件吗 您可能在控制台中遇
$('#myButton').click(function(event) {
event.preventDefault();
$.scrollTo($('#myDiv'), 1000);
});
没有任何进展。您需要为
html,body
演示
您确定正在加载jQuery scrollTo插件文件吗 您可能在控制台中遇到一个object:MethodNotFound“scrollTo”错误 scrollTO方法不是本机jquery方法。要使用它,您需要包含jquery滚动到插件文件 参考: 解决方案: 在head部分添加此项
<script src="\\path\to\the\jquery.scrollTo.js file"></script>
这是我的2美分:
Javascript:
$('.scroll').click(function() {
$('body').animate({
scrollTop: eval($('#' + $(this).attr('target')).offset().top - 70)
}, 1000);
});
Html:
联系人
目标是:
<h2 id="contact">Contact</h2>
联系人
如果要覆盖页面上的标准href id导航,而不更改平滑滚动的HTML标记,请使用以下()命令:
以下是我使用的:
<!-- jquery smooth scroll to id's -->
<script>
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 500);
return false;
}
}
});
});
</script>
$(函数(){
$('a[href*=#]:非([href=#])。单击(函数(){
if(location.pathname.replace(/^\/,'')==this.pathname.replace(/^\/,'')和&location.hostname==this.hostname){
var target=$(this.hash);
target=target.length?target:$('[name='+this.hash.slice(1)+']');
if(目标长度){
$('html,body')。设置动画({
scrollTop:target.offset().top
}, 500);
返回false;
}
}
});
});
这种方法的优点在于,您可以使用无限数量的散列链接和相应的ID,而无需为每个链接执行新的脚本
如果您使用的是WordPress,请将代码插入主题的footer.php
文件,就在结束正文标记的前面
如果您无法访问主题文件,则可以将代码直接嵌入帖子/页面编辑器(您必须以文本模式编辑帖子)或加载到所有页面的文本小部件中
如果您使用的是任何其他CMS或HTML,您可以将代码插入到一个部分中,该部分将加载到所有页面上,正好在结束正文标记
之前
如果您需要有关此的更多详细信息,请查看我的快速帖子:
希望这能有所帮助,如果您有任何疑问,请告诉我 此脚本是对Vector脚本的改进。我对它做了一点改变。因此,该脚本适用于包含类页面滚动的每个链接 首先,不放松:
$("a.page-scroll").click(function() {
var targetDiv = $(this).attr('href');
$('html, body').animate({
scrollTop: $(targetDiv).offset().top
}, 1000);
});
为了简化,您需要Jquery UI:
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
决赛
您可以在这里找到的所有度量:。您可以使用以下简单的jQuery代码来完成 教程、演示和源代码可以在这里找到- JavaScript:
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.substr(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
});
});
<a href="#section1">Go Section 1</a>
<div id="section1">
<!-- Content goes here -->
</div>
HTML:
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.substr(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
});
});
<a href="#section1">Go Section 1</a>
<div id="section1">
<!-- Content goes here -->
</div>
再举一个例子:
HTML链接:
<a href="#featured" class="scrollTo">Learn More</a>
HTML锚定:
<div id="featured">My content here</div>
我的内容在这里
在这里,我尝试了这个,它对我来说非常有用
$('a[href*="#"]').on('click', function (e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr('href')).offset().top
}, 500, 'linear');
});
HTML:
滚动这里
这是最简单的。源代码-
这对我有用
<div id="demo">
<h2>Demo</h2>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
// Handler for .ready() called.
$('html, body').animate({
scrollTop: $('#demo').offset().top
}, 'slow');
});
</script>
演示
$(文档).ready(函数(){
//调用了.ready()的处理程序。
$('html,body')。设置动画({
scrollTop:$('#demo').offset().top
}“慢”);
});
谢谢。此代码对于web上的任何内部链接都很有用
$("[href^='#']").click(function() {
id=$(this).attr("href")
$('html, body').animate({
scrollTop: $(id).offset().top
}, 2000);
});
这里是我的解决方案,使用jQuery平滑滚动到div/anchor,以防有固定的头,这样它就不会滚动到下面。 如果您从其他页面链接它,它也可以工作 只需将“.site header”替换为包含标题的div
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
var headerheight = $(".site-header").outerHeight();
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: (target.offset().top - headerheight)
}, 1000);
return false;
}
}
});
//Executed on page load with URL containing an anchor tag.
if($(location.href.split("#")[1])) {
var headerheight = $(".site-header").outerHeight();
var target = $('#'+location.href.split("#")[1]);
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - headerheight
}, 1);
return false;
}
}
});
普通JS:
如果您使用现代浏览器,可以在纯JS中完成。
文档
.getElementById(“范围计算器”)
.ScrollingToView({行为:“平滑”});
浏览器支持有点问题,但是。您看过这个吗@我买了那辆瓦尼拉,但它看起来太臃肿了,连一个链接都没有use@StevenPHP,我已经根据我的答案替换了您示例中的JavaScript代码。它可以工作。可以在普通的JS中完成-@vector我有一个问题,单击它后,我必须与jquery进行斗争才能向上滚动,有解决方案吗?@yesitsme…在我的case@GraySpectrum向上,只在点击之后,听起来好像有延迟。我有同样的问题,如果我有几个按钮需要滚动到不同的位置怎么办,已尝试修改此代码,但无效。你能提供另一个例子吗?找到了一些“修复”方法。正确元素的滚动现在已经固定,但仍然可以通过单击相同的“滚动到”目标来上下移动:
var target=$(this.data(“目标”)$(“.basics content”).animate({scrollTop:$(target.offset().top},1000);})代码>说明:.basics content
是模态的内部div,我实际上想滚动到它,使用target
我提供元素的id号…这是多么简单和普通,完美。只有在不声明doctype的情况下,此选项才有效。这里的eval
有什么用?我认为它是必需的$('html,body')。动画滚动这应该是公认的答案。问题中的代码是正确的,工作正常。看起来scrollTo插件没有/没有工作。他没有问过做类似事情的不同方法。这很有效,我可以建议一个非常小的调整var pos=$(id).offset().top代码>可以是var pos=$id.offset().top代码>非常好。如果您只希望在某些链接上发生这种情况(比如说您有一些链接要显示或隐藏信息),只需向它们添加一个类名,并将您的类名(比如scroller)添加到匹配声明的末尾(例如a[href^=“#”].scroller).没有jQuery你怎么做?你的答案有什么不同?iOS上的Safari不支持这一点。桌面上的Safari也不支持它(根据MDN),谢谢。这工作得更早,它不是滚动到div的顶部,而是t
<div id="featured">My content here</div>
$('a[href*="#"]').on('click', function (e) {
e.preventDefault();
$('html, body').animate({
scrollTop: $($(this).attr('href')).offset().top
}, 500, 'linear');
});
<a href="#fast-food" class="active" data-toggle="tab" >FAST FOOD</a>
<div id="fast-food">
<p> Scroll Here... </p>
</div>
var elmnt = document.getElementById("content");
elmnt.scrollIntoView();
<div id="demo">
<h2>Demo</h2>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
// Handler for .ready() called.
$('html, body').animate({
scrollTop: $('#demo').offset().top
}, 'slow');
});
</script>
$("[href^='#']").click(function() {
id=$(this).attr("href")
$('html, body').animate({
scrollTop: $(id).offset().top
}, 2000);
});
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
var headerheight = $(".site-header").outerHeight();
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: (target.offset().top - headerheight)
}, 1000);
return false;
}
}
});
//Executed on page load with URL containing an anchor tag.
if($(location.href.split("#")[1])) {
var headerheight = $(".site-header").outerHeight();
var target = $('#'+location.href.split("#")[1]);
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top - headerheight
}, 1);
return false;
}
}
});