How to always show the vertical scrollbar in a browser?

前端 未结 9 543
盖世英雄少女心
盖世英雄少女心 2020-12-01 01:42

I want to always show vertical scrollbar in my webpage. Is it possible using javascript? I think it is possible using javascript or jQuery. I want vertical scrollbar whether

相关标签:
9条回答
  • 2020-12-01 02:21

    try calling a function on the onload method of your body tag and in that function change the style of body like this document.body.style.overflow = 'scroll'; also you might need to set the width of your html as this will show horizontal scroll bars as well

    your html file will look something like this

    <script language="javascript">
        function showscroll() {
            document.body.style.overflow = 'scroll';
        }
    </script>
    </head>
    <body onload="showscroll()">
    
    0 讨论(0)
  • 2020-12-01 02:26

    // Nothing to show here
    body {
      height: 150vh;
    }
    
    ::-webkit-scrollbar {
      width: 15px;
    }
    
    ::-webkit-scrollbar-thumb {
      background: rgba(0, 0, 0, .6);
    }
    
    /* Of course you can style it even more */
    <h1 style="margin: 0;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);font-family: Helvetica, Arial, sans-serif;">Scroll</h1>

    0 讨论(0)
  • 2020-12-01 02:29

    Here is a method. This will not only provide scrollbar container, but will also show the scrollbar even if content isnt enough (as per your requirement). Would also work on Iphone, and android devices as well..

    <style>
        .scrollholder {
            position: relative;
            width: 310px; height: 350px;
            overflow: auto;
            z-index: 1;
        }
    </style>
    
    <script>
        function isTouchDevice() {
            try {
                document.createEvent("TouchEvent");
                return true;
            } catch (e) {
                return false;
            }
        }
    
        function touchScroll(id) {
            if (isTouchDevice()) { //if touch events exist...
                var el = document.getElementById(id);
                var scrollStartPos = 0;
    
                document.getElementById(id).addEventListener("touchstart", function (event) {
                    scrollStartPos = this.scrollTop + event.touches[0].pageY;
                    event.preventDefault();
                }, false);
    
                document.getElementById(id).addEventListener("touchmove", function (event) {
                    this.scrollTop = scrollStartPos - event.touches[0].pageY;
                    event.preventDefault();
                }, false);
            }
        }
    </script>
    
    <body onload="touchScroll('scrollMe')">
    
        <!-- title -->
        <!-- <div class="title" onselectstart="return false">Alarms</div> -->
        <div class="scrollholder" id="scrollMe">
    
    </div>
    </body>
    
    0 讨论(0)
提交回复
热议问题