首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视野之外:在视野中隐藏然后触发

视野之外:在视野中隐藏然后触发
EN

Stack Overflow用户
提问于 2017-08-29 11:29:46
回答 2查看 51关注 0票数 1

成功:以下代码有效。当div滚动到视图中时,它将激活css (显示、动画等)。

问题:我只想让它触发一次(我不想让它再次隐藏或在你滚动过去时重置)。

代码如下:

代码语言:javascript
复制
;(function($) {
  $.outOfView = {
      init: function() {
        $("[data-outofview]").css('transition', 'none').addClass('outofview');
        window.setTimeout(function(){
          $("[data-outofview]").css('transition', '');
          $.outOfView.scroll();
        }, 100);
      }
    , scroll: function() {
       var $window = $(window)
         , scrolled = $(window).scrollTop()
         , windowHeight = $(window).height();

       $("[data-outofview].outofview").each(function() {
         var $this = $(this)
           , dataCoefficient = $(this).data('outofview-coefficient')
           , coefficient = dataCoefficient ? parseInt(dataCoefficient, 10) / 100 : 1
           , windowHeightPadded = windowHeight * coefficient
           , offsetTop = $this.offset().top
           , offsetBottom = offsetTop + $this.outerHeight() * coefficient;

          // If the distance from the bottom of the element to the top of the document
          // is greater than the distance from the top of the window to the top of the
          // document, OR the distance from the top of the element is less than the distance
          // from the bottom of the window to the top of the document... remove the class.
          // The element is in view.
         if ( scrolled < offsetBottom || scrolled + windowHeightPadded > offsetTop) {
           if ($this.data('outofview-timeout')) {
             window.setTimeout(function() {
               $this.removeClass('outofview');
             }, parseInt($this.data('outofview-timeout'), 10));
           } else {
             $this.removeClass('outofview');
           }
         }
       });
       // Hidden...
       $("[data-outofview]:not(.outofview)").each(function () {
         var $this = $(this)
           , dataCoefficient = $(this).data('outofview-coefficient')
           , coefficient = dataCoefficient ? parseInt(dataCoefficient, 10) / 100 : 1
           , windowHeightPadded = windowHeight * coefficient
           , offsetTop = $this.offset().top
           , offsetBottom = offsetTop + $this.outerHeight() * coefficient;

          // If the distance from the bottom of the element to the top of the document
          // is less than the distance from the top of the window to the top of the
          // document, OR the distance from the top of the element is greater than the distance
          // from the bottom of the window to the top of the document... add the class.
          // The element is out of view.
         if ( scrolled > offsetBottom || scrolled + windowHeightPadded < offsetTop) {
           $(this).addClass('outofview');
         }
       });
      }
  };

  // Reveal animations as they appear on screen
  $(window).on('scroll', $.outOfView.scroll);
  $.outOfView.init();
})(jQuery);

如果您能提供任何帮助,我们将不胜感激。虽然我以前有过定制代码,但我不是专家。

EN

回答 2

Stack Overflow用户

发布于 2017-08-29 12:42:47

尝试访问此链接Popup on website load once per session

我认为这就是你使用sessionStorage所需要的,我以前已经尝试过了,并且工作得很好。

票数 0
EN

Stack Overflow用户

发布于 2017-08-30 10:57:31

我不能100%解决这个问题,但我确实有一个可行的解决方案。

在它的核心,我不希望这个隐藏的div再次,一旦项目是看不见(滚动过去)。因此,我删除了以下代码…

代码语言:javascript
复制
scrolled > offsetBottom ||

这本质上意味着脚本将仅在向下滚动时触发。它不会再次隐藏/触发元素,除非您从div上方向下滚动页面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45930246

复制
相关文章

相似问题

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