【问题标题】:JQuery add class on scroll height headerJQuery在滚动高度标题上添加类
【发布时间】:2015-01-19 19:36:59
【问题描述】:

我使用此代码在滚动上添加一个类。活动类效果很好,但是放置类时页面上的位置不正确。我们在我们的网站上使用一个位置固定的主标题,当这个标题变得粘滞时,它被放置在我们的主标题下方,也是固定的。活动类需要在滚动时更早地放置在页面上,因为该部分的内容在放置类时已经开始。

由于某种原因,代码与同一页面上的另一个脚本冲突,我该如何解决这个问题?这两个脚本并排在一起。

小提琴:http://jsfiddle.net/0knrcv3z/1/

HTML:

<div style="height:57px;">
  <div class="menu-header-product">
  <div class="product-anchor-links-wrapper">
  <nav class="product-page-nav">
   <ul class="menu-header-top-product">
    <li class="menu-item-header-product"><a href="#description" class="product-nav-link">Productbeschrijving</a></li>
    <li class="menu-item-header-product"><a href="#additional" class="product-nav-link">Specificaties</a></li>
    <li class="menu-item-header-product"><a href="#reviews" class="product-nav-link">Reviews</a></li>
   </ul>
  </nav>
 </div>
 </div>
</div>

<div class="content">  
<section id="description">
<div class="box-description"></div>
</section>

<section id="additional">
<div class="box-additional"></div>
</section>

<section id="reviews">
<div class="box-reviews"></div>
</section>
</div>

我需要在此代码中进行哪些更改?

<script>
$(window).scroll(function(){
  var sticky = $('.menu-header-product'),
      scroll = $(window).scrollTop();

  if (scroll >= 645) sticky.addClass('sticky');
  else sticky.removeClass('sticky');
});
$(window).scroll(function(){
  var sticky = $('.content'),
      scroll = $(window).scrollTop();

  if (scroll >= 645) sticky.addClass('sticky');
  else sticky.removeClass('sticky');
});
    $(document).ready(function () {
        $(document).on("scroll", onScroll);

        $('a[href^="#"]').on('click', function (e) {
            e.preventDefault();
            $(document).off("scroll");

            $('a').each(function () {
                $(this).removeClass('active');
            })
            $(this).addClass('active');

            var target = this.hash;
            $target = $(target);
            $('html, body').stop().animate({
            'scrollTop': $target.offset().top-130 /**just subtract the height of the fixed html part */
             }, 500, 'swing', function () {
                window.location.hash = target;
                $(document).on("scroll", onScroll);
            });
        });
    });

function onScroll(event){
        var scrollPosition = $(document).scrollTop();
        $('nav a').each(function () {
            var currentLink = $(this);
            var refElement = $(currentLink.attr("href"));
            if (refElement.position().top <= scrollPosition && refElement.position().top + refElement.height() > scrollPosition) {
                $('nav ul li a').removeClass("active");
                currentLink.addClass("active");
            }
            else{
                currentLink.removeClass("active");
            }
        });
    }
</script>

【问题讨论】:

  • 你能用 Fiddle 或 sn-p 把东西包起来吗?

标签: javascript jquery html


【解决方案1】:

if (scroll >= 50) sticky.addClass('sticky');别的 sticky.removeClass('sticky'); }); $(window).scroll(function(){ var 粘性 = $('.content'), 滚动 = $(window).scrollTop();

if (scroll >= 12) sticky.addClass('sticky');别的 sticky.removeClass('sticky'); });

在滚动超过“50”时添加粘贴标题,并在滚动到 12 以下时将其删除。只要对您有好处,只需编辑这些数字;)

【讨论】:

  • 你能用这段代码编辑小提琴吗?因为我试过了,但是代码不行。
  • 我只是稍微编辑了一下,但它并不完美..我会寻找其他东西来修复它
  • 试过了,但该代码并没有给我带来解决方案。你知道另一种解决方案吗?
  • 修复了!将此if (refElement.position().top &lt;= scrollPosition &amp;&amp; refElement.position().top + refElement.height() &gt; scrollPosition) { 改成if (refElement.position().top - 130 &lt;= scrollPosition &amp;&amp; refElement.position().top - 130 + refElement.height() &gt; scrollPosition) {
猜你喜欢
  • 1970-01-01
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多