Scroll the div that is dynamically filled without affecting the scrolling of the main page

My current struggle involves using iScroll in my web project. The goal is to populate a list with articles and slide in a div over the list to display the selected article. While the basic functionality is in place, I face an issue where scrolling through the article also scrolls the list of articles. You can observe this behavior here (the website is in Russian; click on an article and scroll to the end). Below is the code snippet:

<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>

I'm seeking a solution to enable scrolling within the div container, content, or wrapper2 without affecting the scrolling behavior of the wrapper containing the list of articles. It seems that my implementation of iScroll may be incorrect, as I encounter the same issue on both Android and iPhone devices.

UPDATE 1:

Changing the wrapper's position to fixed did manage to isolate scrolling to the div container, but now the list of articles does not scroll. I attempted to add another iScroll for the wrapper, but this approach did not yield the desired outcome. Any suggestions in this regard?

UPDATE 2:

Considering abandoning iScroll in favor of CSS alternatives, I made adjustments to my onclick events:

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

On clicking the close button, the overflow setting reverts to auto. While this effectively prevents scrolling in a browser, the same solution does not apply to mobile devices. How can I achieve consistent behavior across both platforms?

Answer №1

Finally figured it out! To get it to work, I simply added an extra div inside the wrapper div. I'll share the code in the hopes that it can assist someone else. Here's how the new code is structured:

<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

And there you have it! The solution now works seamlessly with two divs requiring iScroll on the same page. Hopefully, this explanation is clear enough and can benefit someone else!!!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Json with React's Context API

I am dealing with nested JSON and arrays within it. My goal is to create a Search functionality that will iterate through the arrays and, based on a specific ID, display the name of the corresponding object in the array. I attempted using the Context API ...

injecting a variable from the configuration service into a TypeScript decorator

I am interested in setting up a scheduled task for my NestJs application to run at regular intervals. I found information on how to use intervals in the NestJs documentation. Since my application uses configuration files, I want to keep the interval value ...

Saving HTML data into the WordPress database with the help of Ajax

After developing a WordPress plugin, I encountered an issue while working with div elements. My goal is to retrieve the content of a specific div using the following code snippet: var archive = j( "div.page2" ).html(); console.log(archive); Everything wo ...

What steps should I take to verify the validity of an Angular form?

I am struggling with validating an inscription form in HTML. Despite trying to implement validations, the inputs are still being saved in the database even when they are empty. Here are the validations I want to include: All inputs are required Email addr ...

Tips for identifying a webpage and making class modifications based on it

I am working on a customized bar with 4 distinct parts. Each part corresponds to a different page on the website. I want each part of the bar to change color based on which page the user is currently browsing. For example, if the user is on page A, I want ...

What is the process for deselecting text from a text field?

After performing a search and displaying the results on my search form, I've noticed that the text query in the textfield is being marked as selected text even though I don't want it to be. How can I prevent this? Fiddle 1) What is the best app ...

Data that is loaded asynchronously will not display rendered

I have async data loading via jayson from a server. After the data has finished loading, the boolean "loading" is set to false but my content does not re-render. Even though I can see on the console that the data was loaded correctly. var App = new Vue( ...

Conceal the slider thumb within the material-ui framework

I've been attempting to conceal the slide thumb, initially without using a library. However, I started considering utilizing material-ui as it might make the process easier. Hence, I'm seeking assistance here. Below is my code snippet: import * ...

Can CSS be used to generate these shadows?

Check out this image I made showcasing a board with an elongated shadow cast behind it. The intention is to emulate the look of the board leaning against a wall, resulting in a triangular shadow on either side. I'm curious if it's achievable to ...

Please verify the utilization of jQuery.ajaxSettings.xhr

var _orgAjax = jQuery.ajaxSettings.xhr; jQuery.ajaxSettings.xhr = function () { var xhr = _orgAjax(); DoSomeLogging(); return xhr; }; I came across this code snippet and I'm trying to make sense of it. Does this mean that any $.ajax(...) ...

Using an HTML ellipsis without setting a specific width

I am working on achieving a specific layout as shown in the screenshot below. If both SPAN and B fit within the box, they should be displayed one after another. If they do not fit, SPAN should have an ellipsis while B is fully displayed (never larger tha ...

What situations warrant the choice of an Isomorphic JavaScript application?

Recently, there has been an increase in tutorials and examples discussing Isomorphic web applications with react.js. As a beginner myself, I find it challenging to determine whether this is necessary or not. For instance, my application will primarily be ...

What causes a component to not update when it is connected to an Array using v-model?

Array1 https://i.stack.imgur.com/cY0XR.jpg Array are both connected to the same Array variable using v-model. However, when changes are made to Array1, Array2 does not update. Why is this happening? Process: Upon examining the logs, it can be observed th ...

Header Dropdown Problems

Hey there, I have a question regarding my header setup. I currently have two headers in place: Whenever I click the notification button, the dropdown menu seems to disrupt the layout of the header. Here is a screenshot for reference: Below is the CSS cod ...

Tips for properly styling the input type range element

Is there a way to fix the issue with my input type="range" styling using linear-gradient when the variable is set to 75%? It seems to be behaving incorrectly. body{ background: green; } .wrapper { width: 20vmin; } input { widt ...

Transmit information from the main directive to the subordinate directive

Hi everyone, I have a quick question.. I'm working on a directive that includes an ng-repeat and transclude, with several child directives inside it that need to inherit specific objects from each iteration... So far, I've only managed to achiev ...

Problem arising from apostrophe usage in Javascript OData request

In my current project, I have a text input field that passes a value through JS to fetch filtered data of names from a JSON file using OData query parameters. However, I've encountered an issue where if a name contains an apostrophe, it results in a ...

Present pop-up messages in the most sophisticated manner

I have successfully created an AngularJS app that is functioning well. Now, I am faced with the challenge of displaying different pop-ups based on specific conditions being met. I am unsure of the best approach to take. Currently, I am considering two op ...

Passing data between parent and child components within an Angular application using mat tab navigation

I am currently working on a project, which can be found at this link. Current Progress: I have implemented a mat tab group with tabs inside the app-component. When a tab is clicked, a specific component is loaded. Initially, most of the data is loaded in ...

What is the best way to programmatically select a checkbox that was created dynamically using jQuery?

I've been scouring the internet trying to find information on what I'm attempting to accomplish, but so far I haven't come across anything helpful. The issue I'm facing is with a form that has a section dynamically added using JavaScri ...