简体   繁体   English

将滚动位置附加到div的不透明度

[英]Attach scroll position to opacity of a div

I have 3 divs, the first is fixed, the second on scroll changes opacity until it's at the same posisiton and then fixes itself. 我有3个div,第一个固定,第二个滚动更改不透明度,直到它处于相同的位置,然后自行修复。 the third waits until the second is positioned then changes it's opacity until it's at the same position and also fixes itself. 第三个等待直到第二个被放置,然后更改其不透明度,直到它位于相同位置并自行修复为止。

The problem is the third div starts always with 50% opacity. 问题是,第三个div总是以50%的不透明度开始。

Here is a JSfiddle - http://jsfiddle.net/topaz85/aeemdptb/4/embedded/result/ 这是一个JSfiddle- http://jsfiddle.net/topaz85/aeemdptb/4/embedded/result/

Here is the script and offending line of code. 这是脚本和有害的代码行。

$("document").ready(function (e) {

var bluetop = $(".pink-bg").outerHeight();
var greentop = $(".pink-bg").outerHeight() + $(".blue-bg").outerHeight();
var footertop = $(".pink-bg").outerHeight() + $(".blue-bg").outerHeight() + $(".green-bg").outerHeight();

var headerHeight = $(".header-container").outerHeight()

$(".blue-bg").css("top", bluetop);
$(".green-bg").css("top", greentop);
$(".approach-section").height(footertop);

$(".pink-bg").css("position", "fixed");

$(".blue-bg, .green-bg").append("<div class='color-bg'></div>");

var bluedistance = $('.blue-bg').offset().top,
    greendistance = $('.green-bg').offset().top;

$(window).scroll(function () {
    var st = $(this).scrollTop();


    if (st + headerHeight >= bluedistance) {
        $(".blue-bg").css({
            "position": "fixed",
            "top": headerHeight
        })
    } else {
        $(".blue-bg").css({
            "position": "absolute",
            "top": bluetop
        })
    }
    if (st + headerHeight >= greendistance) {
        $(".green-bg").css({
            "position": "fixed",
            "top": headerHeight
        })
    } else {
        $(".green-bg").css({
            "position": "absolute",
            "top": greentop
        })
    }

    /* avoid unnecessary call to jQuery function */
    if (st + headerHeight <= bluetop) {
        $(".blue-bg .color-bg").css({
            'opacity': (1 - st / bluetop)
        });
    }

    if (st + headerHeight >= bluetop) {
        var halfScroll = st / 2;
        $(".green-bg .color-bg").css({
            'opacity': (1 - halfScroll / bluetop)
        });
    }

});

}); });

I've looked at this stackoverflow question and have had no luck - Div opacity based on scrollbar position 我看过这个stackoverflow问题,但没有运气- 基于滚动条位置的Div不透明度

Any suggestions? 有什么建议么?

Shouldn't the second if be greentop? 如果不是第二if是greentop?

if (st + headerHeight <= bluetop) {
    $(".blue-bg .color-bg").css({
        'opacity': (1 - st / bluetop)
    });
}

if (st + headerHeight >= greentop) { <!-- CHANGE HERE -->
    var halfScroll = st / 2;
    $(".green-bg .color-bg").css({
        'opacity': (1 - halfScroll / greentop) <!-- CHANGE HERE -->
    });
}

UPDATE : 更新

if (st+headerHeight <= greentop) {
  var halfScroll = st*2;
  $(".green-bg .color-bg").css({ 'opacity' : (2 - halfScroll/greentop) });
}

Played with your settings, all three lines here have some adjustment and it works. 使用您的设置播放,此处的所有三行都进行了一些调整,并且可以正常使用。

See it working ... jsFiddle 看到它的工作... jsFiddle

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

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