Javascript smoothscroll由于某些原因无法工作
仍在我的网站上工作: 有一个向下导航页面的按钮,操作是即时的,但平滑滚动更好 所以,我到处搜索,尝试了很多东西,我发现最短的脚本是这个,但我无法让它工作:Javascript smoothscroll由于某些原因无法工作,javascript,html,Javascript,Html,仍在我的网站上工作: 有一个向下导航页面的按钮,操作是即时的,但平滑滚动更好 所以,我到处搜索,尝试了很多东西,我发现最短的脚本是这个,但我无法让它工作: $(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostnam
$(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
}, 1000);
return false;
}
}
});
});
参考:
以下是我如何在以下两种情况下添加代码的:
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<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
}, 1000);
return false;
}
}
});
});
</script>
</head>
$(函数(){
$('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
}, 1000);
返回false;
}
}
});
});
按钮:
<body>
<a id='button_down' href='#section' onclick="document.getElementById('moodvideo').pause()"><img src='scroll.png' alt='Scroll'></a>
</body>
我检查了给出的示例站点,并以相同的方式将其添加到我的html中。
参考链接:
但是我不能让它工作
另外,我还有另一个脚本,在视频结束后需要同样的动作。
现在的脚本是:
<script>
function videoEnded() {
window.location.href="#section";
}
</script>
函数videoEnded(){
window.location.href=“#section”;
}
这必须做同样的事情;很好地滚动
我希望我的解释可以理解,如果不能理解,我想再试一次。
问候
编辑脚本未添加到联机站点,因为脚本尚未运行,如果这样做更方便,我可以将其添加到联机
更新网站在线,脚本无法正常工作…这篇
要好得多:
$( document ).ready( function () {
$( "a[href*='#']" ).on( "click", function( event ) {
event.preventDefault();
var href = event.target.href;
href = href.slice( href.indexOf( "#" ), href.length );
$( "body" ).animate( {
scrollTop: $( href ).get( 0 ).offsetTop
}, 1000 );
} );
} );
该脚本按预期作用于live copy上的链接,因此我相信您指的是您的
videoEnd()
函数
您发现的平滑滚动脚本仅适用于锚定标记(
)
由于window.location.href=“#section”
不是锚定标记,因此它不会受到脚本的影响
但是,您可以做的是将该脚本的重要部分用在videoEnd()
函数中
函数videoEnded(){
$('html,body')。设置动画({
scrollTop:$(“#节”).offset().top
}, 1000);
}