javascript – 在滚动时隐藏透明div下的内容

前端之家收集整理的这篇文章主要介绍了javascript – 在滚动时隐藏透明div下的内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
代码下方的可能解决方案编辑

该网站有一个全页视频.

滚动操作:不显示位于顶部250像素或更小的内容,因此视频顶部的250像素始终可见.

也许一个更好的方式来理解这一点:隐藏透明div下的内容.但我认为第一个解释是更多的代码相关.

第二个解释导致许多问题和半答案.然而,他们都没有解决我的问题.

这是一个未经答复的问题,涵盖了很多:How to hide contentthat is scrolled under a transparent div?

我更喜欢滚动条全高.

也许这可能是一个提示Add a class to a DIV when top of the window reach a specific element and remove it when not
代码可以检测内容的位置.现在要隐藏这个上溢.

演示
http://jsfiddle.net/4TgmF/

HTML

<div id="header">
    <video autoplay loop poster="https://dl.dropBoxusercontent.com/u/9200106/rsz_dreamstimefree_252880.jpg" id="bgvid">
        <source src="video.mp4" type="video/mp4">
        <source src="video.ogv" type="video/ogg">
    </video>
    <div id="visible_part">Part of the background that shoud be visible at all times. This DIV and its styling is for demonstration only</div>
</div>

<div id="content">
    <p>Lorem ipsum dolor sit amet,consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim,ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet,consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis,pulvinar a semper sed,adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes,nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est.</p>
</div>

CSS

* { margin:0; }
html,body {
  width:100%;
  height:100%;
}
#header {
    width:100%;
    height:100%;
}
#bgvid {
    position:fixed;
    z-index:-1000;
    width:auto;
    height:auto;
    min-width:100%;
    min-height:100%;
}
#visible_part {
    position:fixed;
    height:250px;
    border-bottom:1px solid rgba(255,255,0.1);
    color:#fff;
    background:rgba(0,0.1);
}
#content {
    width:100%;
    min-height:100%;
    background:#fafafa;
}

编辑
Gajus Kuizinas在评论中建议复制背景并将其用作面具,这并不能真正解决问题,但他让我想起了(谢谢).这里的关键词是面具.我发现了一个很好的文章http://thenittygritty.co/css-masking
我认为这可能是一个很好的解决方案.面具将具有位置:固定;顶部:250px;高度:100%;( – 250px).唯一的问题是我不知道如何做一个具有固定位置和可滚动内容的面具.你能看看我的意思吗

解决方法

Here is a working solution in a fiddle.

说明

>将标题放在背景中
>将机身高度设置为标题高度加内容高度
>将内容放在身体底部的包装物中:position:absolute;底部:0
>将内容放在包装的顶部:position:absolute;顶部:0
>设置包装高度以匹配内容高度
>当内容包装的顶部滚动到可见部分的底部时,将其位置更改为固定在该位置:position:fixed;顶部:可见部分的底部
>如果内容包装器是位置:固定,将内容向上移动到其包装器中以继续滚动

大多数这些值都是在JavaScript中设置的,以获得实际的计算值,而不是百分比.这也允许重新计算窗口大小调整.

HTML

<div id="header">
    <video id="bgvid" autoplay loop muted>
        <source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
        <source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
    </video>
</div>

<div id="content_wrapper">            
    <div id="content">
    </div>
</div>

CSS

* { 
    margin:0; 
}
html,body {
    position: relative;
    width:100%;
    height:100%;
}
#header {
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1000;
    width:100%;
    height:100%;
}
#bgvid {
    width:auto;
    height:auto;
    min-width:100%;
    min-height:100%;
}
#content_wrapper {
    position: absolute;
    left: 0px;
    bottom: 0px;
    width: 100%;
    overflow: hidden;    
    z-index: -10;
}
#content {
    background: white;
    position: absolute;
    left: 0px;
    top: 0px;
}

JavaScript(真正的魔法发生)

var $window = $(window);
var $body = $('body');
var $contentWrapper = $('#content_wrapper');
var $content = $('#content');
var minHeaderHeight = 250; // the height of the "visible part"

var lastWindowHeight = $window.height(); // save window height to calculate difference in height on resize

checkScroll(); // make sure scroll and all heights are correct on first load
stickyTop();   // make sure sticky top is used if needed on first load

$window.resize(function() {
    checkScroll();
    stickyTop();
});
$window.scroll(function() {
    stickyTop();
});

function checkScroll() {
    var newWindowHeight = $window.height();
    var windowHeightDif = newWindowHeight - lastWindowHeight;
    lastWindowHeight = newWindowHeight; // save current height for next call

    var contentHeight = $content.height();
    $contentWrapper.height(contentHeight);         // make sure wrapper will show entire content
    $body.height(newWindowHeight + contentHeight); // allow content to be scrolled off the screen by
                                                   // pushing content down by the amount of window height

    var windowScrollTop = $window.scrollTop();
    if (windowScrollTop > 0) {                                // don't scroll if at top to avoid video getting covered
        $window.scrollTop(windowScrollTop + windowHeightDif); // scroll by window height difference to keep content 
                                                              // in the same position on window resize
    }
}

function stickyTop() {
    var windowScrollTop = $window.scrollTop();
    var maxScroll = ($window.height() - minHeaderHeight);
    if (windowScrollTop >= maxScroll) {
        $contentWrapper.css('position','fixed').css('top',minHeaderHeight); // stop wrapper top at bottom of header
    } else {
        $contentWrapper.css('position','absolute').css('top',''); // allow regular scrolling
    }

    if ($contentWrapper.css('position') === 'fixed') {       // if wrapper is fixed,$content.css('top',-(windowScrollTop - maxScroll)); // continue scrolling by shifting content up
    } else {
        $content.css('top',0); // make sure content is lined up with wrapper
    }
}
原文链接:https://www.f2er.com/js/152411.html

猜你在找的JavaScript相关文章