簡體   English   中英

iScroll僅動態填充div而不滾動主頁

[英]iScroll a dynamically filled div ONLY without scrolling main page also

所以這篇文章可能會變得冗長,但我堅持使用iScroll。 我正在做的是用文章填充我的列表,當點擊一個時,我在列表中滑動以顯示文章。 那部分可以工作,但是當我滾動文章並到達最后,它繼續滾動列表與文章。 你可以看看這里 (該網站是俄語,但點擊文章,一直滾動到底部)。 這是我的整個代碼:

<head>
    <style>
        body{
            padding: 0;
            margin: 0;
            border: 0;
        }
        #header{
            position:fixed;
            top:0;
            left:0;
            height:100px;
            width: 100%;
            background-color: black;
        }
        header{
            position: absolute;
            z-index: 2;
            top: 0; left: 0;
            width: 100%;
            height: 50px;
        }
        #wrapper{
            position: absolute;
            z-index: 1;
            width: 100%;
            top: 52px;
            left: 0;
            overflow: auto;
        }
        #container{
            position:fixed;
            top:0;
            right:-100%;
            width:100%;
            height:100%;
            z-index: 10;
            background-color: red;
            overflow: auto;
        }
        #content{
            margin:100px 10px 0px 10px;
        }
    </style>
</head>
<body>
    <header>Main News</header>
    <div id="wrapper">
        <ul id="daily"></ul>
        <ul id="exclusive"></ul>
        <ul id="must"></ul>
        <ul id="main"></ul>
        <ul id="ukr"></ul>
        <ul id="nba"></ul>
        <ul id="euro"></ul>
    </div>
    <div id="container">
        <div id="wrapper2">
            <div id="header">
                <button onclick="hide();">Back</button>
            </div>
            <div id="content"></div>
        </div>
    </div>
    <script src="js/zepto.js"></script>
    <script>
        //AJAX requests to fill the li's...
        function sayhi(url){
            $('#container').animate({
                right:'0',
            }, 200, 'linear');
            $.ajax({
                url: serviceURL + "getnewstext.php",
                data: {link: url},
                success: function(content){
                    $('#content').append(content);
                }
            });
        }
        function hide(){
            $('#container').animate({
                right:'-100%'
            }, 200, 'linear');
            $('#content').empty();
        }
    </script>
    <script src="js/iscroll-lite.js"></script>
    <script>
        var myScroll;
        function scroll () {
            myScroll = new iScroll('wrapper2', {hScroll: false, vScrollbar: false, bounce: false});
            myScroll2 = new iScroll('wrapper', {hScroll: false, vScrollbar: false});
        }
        document.addEventListener('DOMContentLoaded', scroll, false);
    </script>
</body>

有沒有辦法滾動div容器,內容,或wrapper2而不滾動包裝div與文章列表? 也許我沒有正確使用iScroll? Android和iPhone上也會出現同樣的問題。

編輯1:

我將包裝器位置設置為固定。 現在div容器是唯一一個滾動但文章列表不滾動...我添加了另一個iScroll到包裝器,但它不起作用。 有什么建議嗎?

編輯2:

所以我將iScroll全部放在一起並嘗試用CSS代替。 對於我的onclick事件,我補充說:

$('body').css('overflow', 'hidden');

當單擊關閉按鈕時,我將溢出更改為自動。 現在,這可以阻止身體在瀏覽器中滾動,但不能在移動設備上滾動! 我怎樣才能讓它在移動設備上做同樣的事情???

我終於開始工作了。 我需要做的是在包裝器div中添加另一個div。 我將分享代碼,所以希望它可以幫助其他人以下是新代碼的樣子:

<body>
    <!--Added scroller div(without iScroll it works also...just make two divs so the body isn't scrolled but the second div is scrolled-->
    <div id="wrapper">
        <div class="scroller">
            <header>Main News</header>
            <ul id="daily"></ul>
            <ul id="exclusive"></ul>
            <ul id="must"></ul>
            <ul id="main"></ul>
            <ul id="ukr"></ul>
            <ul id="nba"></ul>
            <ul id="euro"></ul>
        </div>
    </div>
    <div id="container">
        <div class="scroller">
            <div id="header">
                <button onclick="hide();">Back</button>
            </div>
            <div id="content"></div>
        </div>
    </div>
<script>
        $('body').on('touchmove', function(e){
            e.preventDefault();
        });
//prevents native scrolling so only iScroll is doing the scrolling
//after the AJAX call to get the content, declare your iScroll variable
var myScroll;
            myScroll = new iScroll('wrapper');
            setTimeout (function(){
                myScroll.refresh();
            }, 2000);

    //set time out to give the page a little time to load the content and refresh your iScroll variable so it takes in the entire content of the wrapper div

var myScroll1;
        myScroll1 = new iScroll('container');
//I defined my second iScroll variable here so I can destroy it in the next part...

//function sayhi(url) stays the same but in success of AJAX looks like this:
success: function(content){
                    $('#content').append(content);
                    myScroll1.destroy();
                    myScroll1 = null;
                    myScroll1 = new iScroll('container');
                    setTimeout (function(){
                        myScroll1.refresh();
                    }, 2000);
                }
//when the div slides on the screen and content gets loaded, destroy your second iScroll
//variable, set it to null and define it all over again so it takes in the entire content

就是這樣。 現在可以完美地使用兩個需要在同一頁面上使用iScroll的div。 希望解釋清楚,並幫助別人!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM