【问题标题】:Scroll down to page, scroll up to top system like on a MEGA.co.nz download page向下滚动到页面,向上滚动到顶部系统,就像在 MEGA.co.nz 下载页面上一样
【发布时间】:2014-01-18 18:39:36
【问题描述】:
var pagestart = 0;
var currentlyat = pagestart;
var lastScrollTop = 0;

$(document).ready(function(){

    function scrollPageTo(a){
        if(a == 0){
    $('#top').show();
    $('#top').animate({
        top: 0
    }, 1000, function(event){
        $('#page').css('top', $(window).height()).hide();
    });
        }
        else
        {
    $('#page').hide();
    $('#page').animate({
        top: 0
    }, 1000, function(event){
        $('#top').css('top', $(window).height()).hide();
    });
        }
    }

    if(pagestart == 0){
        $('#top').css('height', $(window).height());
        $('#page').hide();
    }
    else
    {
        $('#top').hide();
        $('#page').css('height', $(window).height());
    }

    $(window).scroll(function(){
        if(currentlyat == 0){
    if(($(this).scrollTop() < lastScrollTop) && $(this).scrollTop() == 0){
        scrollPageTo(1);
    }
        }
        else
        {
    if(($(this).scrollTop() > lastScrollTop) && $(this).scrollTop() == 0){
        scrollPageTo(0);
    }
        }
    });
});

http://jsbin.com/uZiDaXud/1/edit

我要做的是创建类似于 MEGA.co.nz 网站的系统,例如 this 页面。

基本上是两个容器,其中包含两个单独的页面。一个在#top,另一个在#pagepagestart 确定它应该以 #top 还是 #page 开头。 #top 始终与用户窗口具有相同的高度(因此没有滚动条)。当您在#top 处于活动状态时向下滚动或单击某个链接时,#top 将在屏幕上方向上滚动,#page 将从底部向上滚动。当#page 处于活动状态时(可能比用户的窗口高),并且您位于页面顶部然后仍然向上滚动(或单击链接),#page 将向下滚动到屏幕下方并#top 将从顶部向下滚动。

这将导致一个页面,当您向下滚动时,动画开始将#top 移动到屏幕上方并显示#page,然后您将能够正常滚动。当您位于页面顶部并向上滚动时,#top 将再次弹出。

很难解释,所以我建议点击this 并看到它,因为 MEGA.co.nz 已经实现了它。

我怎样才能达到这个效果?

【问题讨论】:

    标签: javascript jquery css scroll


    【解决方案1】:

    DEMO


    *使用jQuery mouse wheel plugin


    HTML 结构

    <div id="wrapper">
        <div id="splash">
            <div id="splash-footer">Show content</div>
        </div>
        <div id="content">
            <div id="content-header">Show splash</div>
            <div><!-- content here --></div>
        </div>
    </div>
    

    CSS

    /* css normalized */
    html, body {
        height:100%;
        width:100%;
        overflow:hidden;
    }
    #wrapper {
        height:100%;
    }
    #splash {
        position:relative;
        background-color:#cce;
        height:100%;
    }
    #splash-footer {
        position:absolute;
        bottom:0;
        width:100%;
    }
    #content {
        position:relative;
        height:100%;
        overflow:auto;
    }
    #content-header {
        position:absolute;
        top:0;
        width:100%;
    }
    

    jQuery

    /* scroll events */
    $("#splash").on("mousewheel", function (e) {
        if (e.deltaY <= 0) {
            $('#splash').animate({
                height: 0
            }, 500);
        }
    });
    $("#content").on("mousewheel", function (e) {
        if (e.deltaY > 0 && $(this).scrollTop() <= 0) {
            $('#splash').animate({
                height: '100%'
            }, 500);
        }
    });
    
    /* click events */
    $("#splash-footer").on("click", function () {
        $('#splash').animate({
            height: 0
        }, 500);
    });
    $("#content-header").on("click", function () {
        $('#splash').animate({
            height: '100%'
        }, 500);
    });
    

    【讨论】:

    • 感谢您的出色工作。当我想从页面开始而不是从顶部开始时,我可以将#splash的高度设置为0,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多