2015-06-14 16 views
7

在我的網站仍在工作:http://i333180.iris.fhict.nl/p2_vc/的Javascript smoothscroll不工作

沒有爲導航下行TE網頁上的按鈕,動作是即時的,但平滑滾動是好得多。

所以,我谷歌周圍,嘗試了很多,我發現最短的腳本是這樣的: 但我無法得到它的工作。

$(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; 
      } 
     } 
     }); 
    }); 

編號:https://css-tricks.com/snippets/jquery/smooth-scrolling/

這是我如何添加到我的代碼

之間
<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> 

按鈕:

<body> 
    <a id='button_down' href='#section' onclick="document.getElementById('moodvideo').pause()"><img src='scroll.png' alt='Scroll'></a> 
</body> 

我考察的例子給出現場巫婆並將其添加以同樣的方式對我的HTML。 參考檢查鏈接:https://css-tricks.com/examples/SmoothPageScroll/ ,但我不能讓它的工作..

另外,我有一個其他腳本,女巫需要同樣的動作,視頻結束後。 腳本現在是:

<script> 
     function videoEnded() { 
      window.location.href="#section"; 
     } 
    </script> 

這必須做同樣的事情;很好地滾動。

我希望我解釋它可以理解,如果沒有,我想再試一次。 問候

編輯腳本心不是添加到在線網站因爲劇本心不是工作是,如果它將使easer我可以在線添加。

更新網站在線與不工作的腳本

回答

2

該腳本適用於按預期在您的生活將鏈接複製,所以我相信你意思是你的videoEnd()功能。

您找到的平滑滾動腳本僅適用於定位標記(<a>)。由於window.location.href = "#section"不是錨標籤,因此不會受腳本影響。

但是,您可以做的是取出該腳本的重要部分,然後像videoEnd()這樣使用它。

function videoEnded() { 
 
    $('html,body').animate({ 
 
     scrollTop: $("#section").offset().top 
 
    }, 1000); 
 
}

編輯:

它不爲你工作的原因是因爲你瀏覽使用file://協議並鏈接到jQuery是腳本源的頁面

//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js

其中使用了//相關方案,這意味着瀏覽器將追加當前的瀏覽方式,把它變成這個..

file://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js

不存在。如果您指定http://它將起作用

http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
0

這片<script>要好得多:

$(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); 
    }); 
});