javascript - 滚动 div 问题

标签 javascript jquery html css

我正在尝试解决网站上的滚动问题。看看这个 site . 向下滚动会导致向上滑动网站的“隐藏”部分。我想达到同样的效果,它也需要平滑滑动。我试过使用 scrollTop() 和 animate() 但它不起作用。我正在使用 JQuery。让我发布我的代码,这里是:

HTML

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
    <link href="stylin.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
         $(function(){
            $(".button").toggle(function(){
                $("#blocks").animate({top: 200}, 500 );
            },
            function(){ 
                $("#blocks").animate({top: 760}, 500 );
            });
        });
    </script>
 <script type="text/javascript">
 $(document).ready( function() {
     $(document).scroll( function() {
     $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );

    });
});
</script>
    </head>
    <body>
    <div id="main">
        <div id="photo">
        </div>
        <div id="blocks">
            <div id="button_block">
                <a href="#" class="button" ><img src="scrollup.png" /></a>
            </div>
        </div>
    </div>
    </body>
</html>

CSS

    #main {
    width: 1500px; 
    position:absolute;

}

#photo{
    position:fixed;
    top:0;
    width:1500px;
    height:780px;
    background-color:blue;
    z-index:0;

}

#blocks{
    position:absolute;
    top: 760px;
    width: 100%;
    height: 1000px;
    background-color: red;
    z-index:1;
}

#button_block{
    position:absolute;
    width:100%;
    height:64px;
    top:-32px;
}

.button
{
    display:block;
    position: absolute;
    right: 686px;
}

Div blocks 应该是滚动时向上滑动的那个。

看这部分:

<script type="text/javascript">
     $(document).ready( function() {
         $(document).scroll( function() {
         $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );

        });
    });
    </script>

最佳答案

 
#main {
    width: 1500px; 
    position:absolute;

}

#photo{
    position:fixed;
    top:0;
    width:1500px;
    height:780px;
    background-color:blue;
    z-index:0;

}

#blocks{
    position:absolute;
    top: 760px;
    width: 100%;
    height: 1000px;
    background-color: red;
    z-index:1;
}

#button_block{
    position:absolute;
    width:100%;
    height:64px;
    top:-32px;
}

.button
{
    display:block;
    position: absolute;
    right: 686px;
}

#scroll_to_top{
   display:none;
   position:fixed;
   bottom:15px;
   right:20px;
   opacity:0.8;
   z-index:9999;
}
#scroll_to_top:hover{
   opacity:1;
}
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
    <link href="stylin.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
         $(function(){
            $(".button").toggle(function(){
                $("#blocks").animate({top: 200}, 500 );
            },
            function(){ 
                $("#blocks").animate({top: 760}, 500 );
            });
        });
    </script>

    <script type="text/javascript">
        $(function(){
            var stt_is_shown = false;
            $(window).scroll(function(){
            var win_height = 300;
            var scroll_top = $(document).scrollTop(); 
            if (scroll_top > win_height && !stt_is_shown) {
                stt_is_shown = true;
                $("#scroll_to_top").fadeIn();
            } else if (scroll_top < win_height && stt_is_shown) {
                        stt_is_shown = false;
                        $("#scroll_to_top").fadeOut();
                        }else if{
                           <script type="text/javascript">
 $(document).ready( function() {
     $(document).scroll( function() {
     $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );
}
    });
});
</script>
            });
            $("#scroll_to_top").click(function(e){
                e.preventDefault();
                $('html, body').animate({scrollTop:0}, 1000);
            });
        });
 </script>
 <script type="text/javascript">
 $(document).ready( function() {
     $(document).scroll( function() {
     $("#blocks").animate({top: (780-$(this).scrollTop())}, 1000 );

    });
});
</script>
    </head>
    <body>
    <div id="main">
        <div id="photo">
        </div>
        <div id="blocks">
            <div id="button_block">
                <a href="#" class="button" ><img src="scrollup.png" /></a>
            </div>
        </div>
    </div>
    <a href="#" id="scroll_to_top"><img src="scrollup.png" /></a>
    </body>
</html>



   

我更改了 if else 语句并添加了一个新的试试吧!

关于javascript - 滚动 div 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790325/

相关文章:

javascript - 如何以 12 小时 AM/PM 格式添加 24 小时选项?

jquery - ajax 成功函数未触发 MVC

html - Bootstrap .btn-group-justified in a .form-horizo​​ntal

php - 如何访问 require.js 异步加载的 Javascript 中的 URL 参数?

html - 通过悬停或事件防止文本两个像素偏移?

javascript - Selenium 元素不相互作用

javascript - Meteor 实现 Google map 和 Google 地方信息

javascript - 切换选项卡后丢失 setTimeout 值

jquery - jquery 计算总数的百分比

javascript - 循环数组仅捕获第一个元素后,在一个文本字段上在线获取多个数据 仅 JavaScript