简体   繁体   English

在滚动div逐渐淡出淡出

[英]on scroll div gradually fadein fadeout

I am trying to creating fadein fadeout effect with scrolling but not figure out how . 我试图用滚动创建fadein淡出效果,但不知道如何。 it is like http://www.popsci.com/ . 它就像http://www.popsci.com/ There If scroll then background image (div id fixed-image ) get fading. 如果滚动然后背景图像(div id fixed-image)变得褪色。 Them code something like. 他们的代码就像。 But I cannot figure out yet how to easily apply in my code. 但我还不知道如何在我的代码中轻松应用。 Would guys please check it my code . 伙计们请检查我的代码。

var opacity = 1;
      var img = element.find('img');
      // Look for featured stories.
      if (element.length > 0) {
        // Attach background image element.
        $('#page-wrapper').prepend('<div id="fixed-image" style="background-image: url(\'' + img.attr('data-lgsrc') + '\');"></div>');
        img.addClass('hidden');
        var scrollPercent;
        $(window).scroll(function() {
          // When User scrolls down, determine percentage from top to bottom of window.
          scrollPercent = (($(window).scrollTop() / $(window).height() * 1.9) - 0.9);
          if (scrollPercent < 0) {
            $('#fixed-image').css({
              '-webkit-filter'  : 'blur(0px)',
              '-moz-filter'     : 'blur(0px)',
              '-o-filter'       : 'blur(0px)',
              '-ms-filter'      : 'blur(0px)',
              'filter'          : 'blur(0px)'
            });
          }
          var opacityCount = 1.5 - Math.min(1.5, (scrollPercent + 1));
          $('#fixed-image').css('opacity', opacityCount);
          if (scrollPercent <= 1) {
            $('#fixed-image').css('opacity', opacityCount);
            $('#fixed-image').css({
              '-webkit-filter'  : 'blur(' + scrollPercent * 10 + 'px)',
              '-moz-filter'     : 'blur(' + scrollPercent * 10 + 'px)',
              '-o-filter'       : 'blur(' + scrollPercent * 10 + 'px)',
              '-ms-filter'      : 'blur(' + scrollPercent * 10 + 'px)',
              'filter'          : 'blur(' + scrollPercent * 10 + 'px)'
            });
          }
          else {
            $('.content-wrapper-outer').css('background-color', 'rgba(255,255,255,' + opacity + ')');

my demo is here https://jsfiddle.net/cyber007/6e6dbr6j/1/ 我的演示在这里https://jsfiddle.net/cyber007/6e6dbr6j/1/

i want slider class will be fadeIn and fadeout gradually base on scrolling 我希望滑块类将逐渐基于滚动淡入淡出和淡出

UPDATE UPDATE

$(window).scroll(function(){
    $(".slider").css("opacity", 1 - $(window).scrollTop() / 250);
  });

https://jsfiddle.net/cyber007/6e6dbr6j/2/ this one work fine. https://jsfiddle.net/cyber007/6e6dbr6j/2/这个工作正常。 jus one minor think in console i saw that opacity value keep running after 0 even going - value. jus一个未成年人认为在控制台中我看到不透明度值继续运行0甚至去-值。 i don't think after value 0 no need down value more 我不认为在价值0之后不需要更多的价值

Here's a function to clip opacity at 0, although according to MDN : "Any value outside the interval [0 to 1], though valid, is clamped to the nearest limit in the range," so it's not strictly necessary. 这是一个将不透明度剪辑为0的函数,尽管根据MDN :“区间[0到1]之外的任何值虽然有效,但仍被限制在范围内的最近限制,”因此它不是绝对必要的。

$(window).scroll(function(){
    var opacity = 1 - $(window).scrollTop() / 250;
    if (opacity < 0 ) opacity = 0;
    $(".slider").css("opacity", opacity);
});

UPDATE UPDATE

For arbitrary start and end of your transition, use the linear equation 对于过渡的任意开始和结束,请使用线性方程 在此输入图像描述 , like so: ,像这样:

$(window).scroll(function(){
    var start = 200, end = 600;
    var opacity = 1 - ($(window).scrollTop() - start) / (end - start);
    if (opacity > 1 ) opacity = 1;
    if (opacity < 0 ) opacity = 0;
    $(".slider").css("opacity", opacity);
  });

Here's a JSFiddle. 这是一个JSFiddle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM