javascript - 要在此处添加什么 js 代码以获得平滑的滚动?

标签 javascript php jquery html css

<分区>

请检查下面的 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 链接 - readmore

最佳答案

您可以在 showTab()

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

关于javascript - 要在此处添加什么 js 代码以获得平滑的滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41391101/

相关文章:

javascript - 一次组合两个Javascript中的字符串数组

javascript - 如何检测图像路径是否损坏?

javascript - 重新解析从部分 View 创建的表单后,jQuery 提交事件未触发

javascript - jQuery TimePicker 主题不工作

javascript - 在粘贴事件 Javascript 中删除输入字段中的所有非数字字符

javascript - 如何使 Ember 计算属性依赖于变量的所有后代属性?

PHP MySQL从下拉框选择中插入多个值

php - Doctrine 2 保留实体并从插入查询中排除空字段

php - 如何防止最多的投票作弊者?

javascript - 添加动态 html 行后如何选择文本输入