0
$(document).ready(function(){
// Add smooth scrolling
$('.button').children().onclick(function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 1000, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
$(document).ready(function(){
// Add smooth scrolling to all links
$('.button').children().on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 1000, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
當我使用onclick()函數時,單擊按鈕時沒有滾動效果;它只跳轉到沒有任何滾動效果的文章。onclick()和.on('click',function())之間的區別?
但是,當我使用('點擊',功能())有一個滾動效果。
這兩者有什麼區別?
回答了幾次......這裏:https://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick – magreenberg
這個問題是不一樣的。這是比較'addEventListener()'到'onclick()'。這個問題特別針對jQuery的方法。 –