【问题标题】:Static Frame Around Page Which Changes Colors As Scroll页面周围的静态框架随着滚动而改变颜色
【发布时间】:2014-05-21 23:00:23
【问题描述】:

我有一个难以描述的设计,我花了几个小时在谷歌上搜索它,但一无所获。该设计基本上使用了一个固定的边框框架,当内容滚动通过它时,它始终停留在页面上,多个完整的视口“幻灯片”垂直堆叠。每张幻灯片都有一个不同的背景图像,该背景图像也通过 background-size:cover 固定用于视差效果。当您向下滚动内容到下一张幻灯片时,边框颜色应随内容而变化,以便与下一张幻灯片的背景图像一起使用。所以基本上侧边框需要同时是两种颜色,或者有一种颜色覆盖另一种颜色。附上的图片应该会让事情更清楚。

我要开始的是这个内容安排在我所说的幻灯片中:http://jsfiddle.net/4wtRv/

HTML

    <section class="dark" style="background:url('http://www.mccullagh.org/db9/10d-2/new-york-city-at-night.jpg') no-repeat fixed;background-size:cover;">
        <div class="brdr_out">
            <div class="brdr_in">
                <div class="content" style="height:10em;margin-top:-5em;">
                    <div class="title1">TITLE 1</div>
                </div>
            </div>
        </div>
    </section>
    <section class="light" style="background:url('http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg') no-repeat fixed;background-size:cover;">
        <div class="brdr_out">
            <div class="brdr_in">
                <div class="content" style="height:10em;margin-top:-5em;">
                    <div class="title2">Title 2</div>
                    <div class="title3">Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </div>
                </div>
            </div>
        </div>
    </section>

CSS

body, html
{
    height: 100%;
}
body
{
    font-size: 16px;
    margin: 0;
}
.brdr_in, .brdr_out
{
    bottom: 0;
    height: auto;
    left: 0;
    position: absolute;
    right: 0;
    top: 0;
}
.brdr_in
{
    margin: .25em;
    padding: 1em;
}
.brdr_out
{
    margin: 1em;
    padding: .25em;
}
.clr1, .dark
{
    color: #fff;
}
.clr2, .light
{
    color: #000;
}
.dark .brdr_in
{
    border: 3px solid #d5d7a1;
}
.dark .brdr_out
{
    border: 5px solid #d5d7a1;
}
.light .brdr_in
{
    border: 3px solid #000;
}
.light .brdr_out
{
    border: 5px solid #000;
}
section
{
    height: 100%;
    position: relative;
    text-align: center;
}
section .content
{
    position: absolute;
    top: 50%;
    width: 100%;
}
.title1, .title2
{
    display: inline-block;
    letter-spacing: .25em;
    line-height: 1.875em;
    padding-bottom: .8em;
}
.title2
{
    border-bottom: 1px solid #4a4639;
    margin-bottom: 3em;
}

但棘手的部分是在滚动时让它看起来像这样:

此外,文本需要隐藏在框架外的边距中。

非常感谢您的帮助!当我告诉设计师我可以做到时,这比我预期的要困难得多。 Javascript 和它的任何库都很好。谢谢!

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    很难将框架分成两种颜色。我能想象的最简单的方法是制作两个框架副本。给两个固定位置,一个顶部:0,另一个底部:0,并在滚动时调整两者的高度,使它们恰好在中间相遇。

    另一种解决方案,不符合您的规范,但可能看起来更好,是将边框颜色从一张幻灯片淡化到下一张幻灯片。您将需要一个颜色插值函数、JavaScript 中的幻灯片颜色列表和一个滚动处理函数,该函数确定哪张幻灯片可见以及滚动到下一张幻灯片的距离。有点像这样(假设 Jquery 和 1000px 幻灯片):

    $(window).on('scroll', function(){
        var slide_distance = window.scrollY / 1000, slide = Math.floor(slide_distance)
        var color = color_interp(slide_colors[slide], slide_colors[slide + 1], slide_distance - slide)
        $('#border').css('color', color)
    })
    

    【讨论】:

    • 太棒了!我使用这种固定定位和调整顶部和底部的方法让它工作。非常感谢!将在下面的另一个答案中发布示例代码。
    【解决方案2】:

    这是解决此问题的粗略示例代码。我必须让每个边框都是一个单独的 div,这样它就不会覆盖文本、链接等。为了在框架上下滚动时阻止文本,我添加了一个元素,该元素显示相同的背景图像,具有更高的 z-索引,因此它覆盖了框架之外的部分。

    HTML

        <section class="dark" style="background:url('http://www.mccullagh.org/db9/10d-2/new-york-city-at-night.jpg') no-repeat fixed;background-size:cover;">
            <div class="content" style="height:10em;margin-top:-5em;">
                <div class="title1">TITLE 1</div>
            </div>
       </section>
       <section class="light" style="background:url('http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg') no-repeat fixed;background-size:cover;">
            <div class="content" style="height:10em;margin-top:-5em;padding:0 20%;width:60%">
                <div class="title2">Title 2</div>
                <div class="title3">Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </div>
            </div>
       </section>
    
        <div id="top" style="background:url('http://www.mccullagh.org/db9/10d-2/new-york-city-at-night.jpg') no-repeat fixed;background-size:cover;display:none;height:24px;position:fixed;top:0;left:0;right:0;z-index:3;"></div>
        <div id="bottom" style="background:url('http://images.nationalgeographic.com/wpf/media-live/photos/000/004/cache/african-elephant_435_600x450.jpg') no-repeat fixed;background-size:cover;display:none;height:24px;position:fixed;bottom:0;left:0;right:0;z-index:3;"></div>
    
        <div class="brdr_out">
            <div class="dark">
                <div class="top"></div>
                <div class="right"></div>
                <div class="bottom"></div>
                <div class="left"></div>
            </div>
            <div class="light">
                <div class="top"></div>
                <div class="right"></div>
                <div class="bottom"></div>
                <div class="left"></div>
            </div>
        </div>
        <div class="brdr_in">
            <div class="dark">
                <div class="top"></div>
                <div class="right"></div>
                <div class="bottom"></div>
                <div class="left"></div>
            </div>
            <div class="light">
                <div class="top"></div>
                <div class="right"></div>
                <div class="bottom"></div>
                <div class="left"></div>
            </div>
        </div>
    

    CSS

    body, html
    {
        height: 100%;
    }
    body
    {
        font-size: 16px;
        margin: 0;
    }
    .clr1, .dark
    {
        color: #fff;
    }
    .clr2, .light
    {
        color: #000;
    }
    section
    {
        height: 100%;
        position: relative;
        text-align: center;
    }
    section .content
    {
        position: absolute;
        top: 50%;
        width: 100%;
    }
    .title1, .title2
    {
        display: inline-block;
        letter-spacing: .25em;
        line-height: 1.875em;
        padding-bottom: .8em;
    }
    .title2
    {
        border-bottom: 1px solid #4a4639;
        margin-bottom: 3em;
    }
    
    /*The Frame Styles*/
    .brdr_in .bottom
    {
        bottom: 24px;
    }
    .brdr_in .bottom, .brdr_in .top
    {
        height: 3px;
        left: 24px;
        right: 24px;
    }
    .brdr_in .dark div, .brdr_out .dark div
    {
        background-color: #e4d7b0;
    }
    .brdr_in div, .brdr_out div
    {
        position: fixed;
        z-index: 4;
    }
    .brdr_in .left
    {
        left: 24px;
    }
    .brdr_in .left, .brdr_in .right
    {
        bottom: 24px;
        top: 24px;
        width: 3px;
    }
    .brdr_in .light div, .brdr_out .light div
    {
        background-color: #434345;
    }
    .brdr_in .right
    {
        right: 24px;
    }
    .brdr_in .top
    {
        top: 24px;
    }
    .brdr_out .bottom
    {
        bottom: 15px;
    }
    .brdr_out .bottom, .brdr_out .top
    {
        height: 5px;
        left: 15px;
        right: 15px;
    }
    .brdr_out .left
    {
        left: 15px;
    }
    .brdr_out .left, .brdr_out .right
    {
        bottom: 15px;
        top: 15px;
        width: 5px;
    }
    .brdr_out .right
    {
        right: 15px;
    }
    .brdr_out .top
    {
        top: 15px;
    }
    

    JS

           function borders() {
                var viewportHeight = $(window).height();
                var scrollY = window.scrollY;
                var distance = viewportHeight - scrollY;
    
                //Once we start scrolling, the top border of the next slide needs to be hidden so it doesn't appear over content
                if (scrollY >= 0) {
                    $('.light .top').css('display', 'none');
                }
                if (scrollY < 27) {
                    $('.brdr_in .light .bottom').css('display', 'none');
                }
                else {
                    $('.brdr_in .light .bottom').css('display', 'block');
                }
                if (scrollY < 20) {
                    $('.brdr_out .light .bottom').css('display', 'none');
                }
                else {
                    $('.brdr_out .light .bottom').css('display', 'block');
                }
    
                var outerTop = distance;
                var innerTop = distance;
    
                //We've scrolled enough so that the top of the bottom slide reaches the top of the viewport, need to add top border back in
                if (outerTop < 15) {
                    $('.brdr_out .light .top').css('display', 'block');
                    outerTop = 15;
                }
                if (innerTop < 24) {
                    $('.brdr_in .light .top').css('display', 'block');
                    innerTop = 24;
                }
    
                $('.brdr_out .light .left, .brdr_out .light .right').css('top', outerTop);
                $('.brdr_in .light  .left, .brdr_in .light .right').css('top', innerTop);
    
    
                //Add the background image to top/bottom to hide the text as it scrolls under/above it
                if ($('#light .brdr_out').css('border-top-width') == '0px') {
                    $('#top').css('display', 'block');
                }
                else {
                    $('#top').css('display', 'none');
                }
                if (scrollY > 24) {
                    $('#bottom').css('display', 'block');
                }
                else {
                    $('#bottom').css('display', 'none');
                }
           }
    
           $(window).load(function () {
                borders();
           });
    
           $(window).scroll(function () {
                borders();
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多