【问题标题】:Issue with javascript when scrolling via menu buttons通过菜单按钮滚动时出现javascript问题
【发布时间】:2017-08-11 01:19:48
【问题描述】:

我设置了一个简单的单页网站,当您单击每个菜单项时,页面会平滑滚动到该特定部分。那部分我工作得很好......

加载部分后,会显示图像,并且文本会在图像上慢慢向上滚动,这在第一部分(最高部分)上工作得非常好。

当您单击第二个、第三个和第四个菜单链接时,我遇到的问题是,一旦页面移动到页面的该部分,由于上面的部分也加载滚动,内容会继续向上滚动。

这是我用来加载文本以向上滚动的 javascript:

    $(document).ready(function() {

    /* Every time the window is scrolled ... */
    $(window).scroll( function(){

        /* Check the location of each desired element */
        $('.content-scroll').each( function(i){

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* Adjust the "200" to either have a delay or that the content starts fading a bit before you reach it  */
            bottom_of_window = bottom_of_window + 800; 

            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){

                $(this).animate({'margin-top':'0'},10000);

            }

        }); 

    });

});

CSS:

.content-scroll { margin-top: 1000px; }

谁能推荐最好的方法,这样当我点击第二、第三和第四部分链接时,它会跳转到该部分,上面的内容会立即加载,而无需向上滚动?

感谢任何反馈。谢谢

编辑:事先我为每个链接输入了上面的代码(.about-sectionservices-section 等),但意识到我只需要输入一次。

不过,加载该部分后,其中的内容会向上滚动。

任何反馈表示赞赏:)

编辑 2:我已经建立了一个测试网站,显示滚动问题 http://test.flixonstudios.co.uk - 在这里你可以清楚地看到我的意思。

【问题讨论】:

  • 您在代码中说检查每个位置并使用 $('.about-scroll') 但您随后还提到它们的名称不同。 services-scroll, .shop-scroll, .contact-scroll,所以这只会检查 about-scroll,这也是一个滚动事件,你在点击一个元素时提到过,所以每次它都会激活卷轴哪个cna会引起大问题???您能否添加更多详细信息和您的 html 等,以便我们进一步调试,
  • @SimonDavies 代码输入了 4 次(可能不是最好的方法),但这就是我目前设置它的方式,这似乎在内容向上滚动方面起作用,但正如我提到,当您单击第二个、第三个或第四个链接时,上面的内容会向上滚动,因此一旦您到达这些特定部分之一,内容就会继续移动。
  • 我认为是因为它在 SCROLL 事件中,每次窗口滚动时都会触发它,所以它会不断滚动并激活代码,可能需要查看并重做?看看这个stackoverflow.com/questions/18071046/…
  • 基本上你需要在每个链接上附加一个点击事件,然后调用滚动函数而不是滚动事件。看到很多帖子
  • @SimonDavies - 该链接与平滑滚动有关吗?正如我所说,该部分很好,因为当您单击链接时,它会滚动到所需的部分......只是当它到达该部分时,内容然后继续向上滚动......我知道我没有这样做最好的方法,我希望对更好的方法有一些建议...感谢您的反馈:)

标签: javascript jquery html css


【解决方案1】:

我有这个代码笔,我不久前做过

https://codepen.io/simondavies/pen/WRXOZw

$('#navigation').on('click', function(evt){
  evt.preventDefault();
  var gotoSection = evt.target.dataset.scrollto;
  var scrollPos = document.querySelector('#'+ gotoSection).offsetTop;
  $('html,body').animate({scrollTop: scrollPos },1000,'linear');
  
});

$('p').on('click','a.back',function(evt){
  evt.preventDefault();
  $('html,body').animate({scrollTop: 0 },1000,'linear');
  
})
.sections {
  margin: 0 auto;
  padding:30px;
  height: 100Vh;
  background: #d9d9d9;
   display: table;
  verticle-align: middle;
}
.two {
  background: #e9e9e9;
}
p {
  margin: 0 auto;
  text-align: center;
  font-family: helvetica;
  font-size: 20px;
  line-height: 30px;
  font-wieght: normal;
  display: table-cell;
  vertical-align: middle;
  height: 100%;
}
nav {
  margin: 0 auto;
  padding-top: 10px;
  text-align: center;
  width: 100%;
  height: 40px;
  position: fixed;
  z-index: 10;
  top: 0;
  background: #555;
}
a {
  margin: 0 auto;
  text-align: center;
  font-family: helvetica;
  font-size: 20px;
  line-height: 30px;
  font-wieght: normal;
  display: inline-block;
  height: 30px;
  width: 200px;
  text-decoration: none;
  color: #fff;
  transition: color 500ms;
}

a:hover {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav id="navigation">
<a href="#" data-scrollto="section-one">Section One</a> | <a href="#" data-scrollto="section-two">Section Two</a> | <a href="#" data-scrollto="section-three">Section three</a>
</nav>
<div class="sections" id="section-one">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Natus neque, ducimus pariatur odio architecto excepturi, enim non recusandae, temporibus facilis eveniet nulla quo laudantium, alias quos in aliquam delectus dolore!<br /><br /><a href="#" class="back">Back to top</a></p>
</div>
<div class="sections two" id="section-two">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Natus neque, ducimus pariatur odio architecto excepturi, enim non recusandae, temporibus facilis eveniet nulla quo laudantium, alias quos in aliquam delectus dolore!<br /><br /><a href="#" class="back">Back to top</a></p>
</div>
<div class="sections" id="section-three">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Natus neque, ducimus pariatur odio architecto excepturi, enim non recusandae, temporibus facilis eveniet nulla quo laudantium, alias quos in aliquam delectus dolore!<br /><br /><a href="#" class="back">Back to top</a></p>
</div>

【讨论】:

  • 虽然这是一段有用的代码,但我认为您可能忽略了要点......文本内容在该部分已经加载时向上滚动。因此,当您单击其中一个链接时,它会平滑地跳转到该部分,并显示图像,文本会慢慢向上滚动……因此,当您单击第二个,第三个或第四个链接时,它会跳转到该部分,并且那么上面的内容仍在向上滚动,导致该部分向上滚动......希望这有点道理:) 干杯
  • 所以滚动的是内部内容。不过,您认为这可能是滚动事件吗?但是如果没有更深入的示例或图像,我很抱歉
  • 是的,这是正确的,内部内容正在向上滚动,例如,一旦点击了关于链接,关于我们的文本就会从屏幕底部向上滚动,并且加载了关于部分。 . 很遗憾,我无法向您展示我的意思的示例 :( - 感谢您的帮助。
  • 没问题,您是否尝试过使用 embed code in here 选项来添加您正在尝试做的工作示例,这样可以更容易调试。或发布您拥有的所有内容等。仍然认为这是滚动事件 :-) 我现在要走了,尽管希望它已完成。
  • 我已经建立了一个测试网站,显示滚动问题test.flixonstudios.co.uk - 在这里你可以清楚地看到我的意思...仍然没有解决这个问题! :(
猜你喜欢
  • 1970-01-01
  • 2019-05-28
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2021-05-24
  • 1970-01-01
  • 2011-03-09
  • 2021-10-20
相关资源
最近更新 更多