Imitation Taobao sidebar scroll case:

Imitation Taobao scrolling sidebar

Case situation:

  1. Originally sidebar is absolute positioning
  2. When the page scroll to a certain position, the sidebar changed to a fixed positioning
  3. Page continues to roll, will be displayed Back to top

case study:

  1. Need to use the page scroll scroll event because it is the page scrolling, the event is a source document
  2. Scroll to a position, the upper value of the page is determined to be the volume.
  3. Page rolled to head: If you can get is to the left window.pageXOffset volumes by window.pageYOffset
  4. Note that the element is rolled to the head is element.scrollTop, if the page is rolled to the head is window.pageYOffset
  5. This value can be obtained by offsetTop box, if this value is greater than or equal, so that the box can be positioned in a fixed
 
<!DOCTYPE html>
<html lang="en">

 

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <Title> imitation Taobao sidebar </ title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .slider_box {
            position: absolute;
            left: 50%;
            margin-left: 550px;
            top: 456px;
            z-index: 3;
            /* margin-left: 600px; */
        }
        
        .slider_box li {
            width: 50px;
            height: 50px;
            list-style: none;
            background-color: pink;
            border: 1px solid #ccc;
            text-align: center;
            line-height: 50px;
            font-size: 12px;
        }
        
        .container {
            position: relative;
            width: 1200px;
        }
        
        .slider_box .backward {
            display: none;
        }
        
        .title {
            width: 1200px;
            height: 100px;
            background-color: pink;
        }
        
        .header {
            width: 1200px;
            height: 300px;
            background-color: chocolate;
        }
        
        .banner {
            margin-top: 10px;
            width: 1200px;
            height: 500px;
            background-color: blue;
        }
        
        .main {
            margin-top: 10px;
            width: 1200px;
            height: 800px;
            background-color: chartreuse;
        }
    </style>
</head>

<body>

    <div class="slider_box">
        <ul>
            <li></li>
            <Li class = "backward"> Top </ li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <div class="container">
        <div class="title"></div>
        <div class="header">header</div>
        <div class="banner">banner</div>
        <div class="main">main</div>
    </div>
 
    <script>
        1 // Get element
        var slider_box = document.querySelector('.slider_box');
        var banner = document.querySelector('.banner');
        var main = document.querySelector('.main');
        var backward = document.querySelector('.backward')
            // banner.offestTop is the size of the volume to the head must be written scroll out
        was bannerh = banner.offsetTop;
        was sliderh = slider_box.offsetTop;
        was bhight = sliderh - bannerh;
        var mainh = main.offsetTop;
        // 2. Scroll event page scroll
        document.addEventListener('scroll', function() {
            // 3. When are we page volume greater than or equal to the height of the head banner and sidebar will changed to a fixed positioning
            if (window.pageYOffset >= bannerh) {
                slider_box.style.position = 'fixed';
                slider_box.style.top = bhight + 'px';
            } else {
                slider_box.style.position = 'absolute';
                slider_box.style.top = sliderh + 'px';
            }
            // 4. When we scroll the page to the main box is displayed backward module
            if (window.pageYOffset >= mainh) {
                backward.style.display = 'block';
            } else {
                backward.style.display = 'none';
            }
        })
    </script>
</body>

 

</html>

 

Guess you like

Origin www.cnblogs.com/rainbowupdate/p/12552796.html