在这里添加什么JS代码可以实现平滑滚动?

3

请检查以下JS代码,并建议我在javascript中进行进一步添加或修改 -

JS:

var $tabs = $('.tabs > div'), _currhash, $currTab;
$tabs.first().addClass("active");

function showTab() {
   if($currTab.length>0) {  
     $tabs.removeClass('active');
     $currTab.addClass('active');
   }
/* find the panels and 'unlink' the id to prevent page jump */
$tabs.each(function() {
   var _id = $(this).attr('id');
   $(this).attr('id',_id+'_tab');
   /* eg we have given the tab an id of 'tab1_tab' */
}); /* set up an anchor 'watch' for the panels */
function anchorWatch() {
  if(document.location.hash.length>0) {
    /* only run if 'hash' has changed */
    if(_currhash!==document.location.hash) {
       _currhash = document.location.hash; /* we only want to match the unlinked id's */
       $currTab = $(_currhash+'_tab');
       showTab();
   }
  }
} 
setInterval(anchorWatch,300);

jsfiddle无法运行,请检查HTML和CSS链接 - 阅读更多


你好Punit,感谢你的回复伙计。但是它没有起作用,你能否通过单击“阅读更多”链接来测试它?我已经将整个JS代码添加到单个HTML文件中。 - Grapy
1个回答

0

你可以在你的showTab()中尝试这个。

$('html, body').animate({
    scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);

抱歉伙计,但它不起作用。 - Grapy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接