首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >卷顶偏移顶问题

卷顶偏移顶问题
EN

Stack Overflow用户
提问于 2019-03-21 10:23:52
回答 1查看 1.7K关注 0票数 1

我使用下面提到的代码滚动到div。我在移动视图中应用了scrollTop: $(target).offset().top-250,这样该部分将显示在标题高度以下,但它似乎不起作用。

代码语言:javascript
运行
复制
$('#portfolioNavbar ul li a[href*=#]').bind('click', function(e) {
  e.preventDefault();
  var target = $(this).attr("href");

  $('html, body').stop().animate({
    scrollTop: $(target).offset().top - 250
  }, 600, function() {
    location.hash = target; //attach the hash (#jumptarget) to the pageurl
  });

  return false;
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-21 11:53:20

下面的脚本节省了我的时间。这个脚本可能对其他人有用。

代码语言:javascript
运行
复制
$('#portfolioNavbar ul li a').click(function() {
     var tghsh = $(this).attr('href').substring(1);
     var headerHeight = $('.portfolioi_left').outerHeight();
     var winwid = $(window).width();
     var doffset = $('#'+tghsh).offset().top-20;
     var doffset1 = $('#'+tghsh).offset().top-280;
     if(winwid <= 991) {
         doffset1 -= headerHeight;
     $('html, body').animate({scrollTop:doffset1},700);
     return false;
     }
     $('html, body').animate({scrollTop:doffset},700);
     return false;
});

如果要使菜单活动的滚动到div。你能写这个剧本吗?

代码语言:javascript
运行
复制
$(document).ready(function() {
$('#portfolioNavbar ul li a').click(function() {
     $(document).off("scroll");

    $('#portfolioNavbar ul li a').each(function () {
        $(this).removeClass('active');
    })
    $(this).addClass('active');
     var tghsh = $(this).attr('href').substring(1);
     var headerHeight = $('.portfolioi_left').outerHeight();
     var winwid = $(window).width();
     var doffset = $('#'+tghsh).offset().top-20;
     var doffset1 = $('#'+tghsh).offset().top-280;
     if(winwid <= 991) {
         doffset1 -= headerHeight;
     $('html, body').animate({scrollTop:doffset1},700);
     return false;
     }
     $('html, body').animate({scrollTop:doffset},700);
     return false;
});
});

function onScroll(event){
    var scrollPos = $(document).scrollTop();
    $('#portfolioNavbar ul li a').each(function () {
    var currLink = $(this);
    var refElement = $(currLink.attr("href"));
    if (refElement.position().top <= scrollPos && refElement.position().top + refElement.height() > scrollPos) {
        $('#portfolioNavbar ul li a').removeClass("active");
        currLink.addClass("active");
    }
    else{
        currLink.removeClass("active");
    }
});
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55278273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档