慕容森
2019-10-05 13:27:30
我一直在嘗試滾動到div id jquery代碼以正常工作?;诹硪粋€堆棧溢出問題,我嘗試了以下操作演示http://jsfiddle.net/kevinPHPkevin/8tLdq/$('#myButton').click(function() { $.scrollTo($('#myDiv'), 1000);});但這沒有用。它只是捕捉到div。我也試過$('#myButton').click(function(event) { event.preventDefault(); $.scrollTo($('#myDiv'), 1000);});沒有進展。
3 回答

MMMHUHU
TA貢獻1834條經驗 獲得超8個贊
如果您想覆蓋頁面上的標準href-id導航而不更改HTML標記以進行平滑滾動,請使用以下示例(示例):
// handle links with @href started with '#' only
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
if ($id.length === 0) {
return;
}
// prevent standard hash navigation (avoid blinking in IE)
e.preventDefault();
// top position relative to the document
var pos = $id.offset().top;
// animated top scrolling
$('body, html').animate({scrollTop: pos});
});
添加回答
舉報
0/150
提交
取消