Scroll a div from everywhere

后端 未结 2 1804
梦如初夏
梦如初夏 2020-12-12 06:30

I do not know how to describe my concern and thats why I could not find anything on Google. I want to draw a div is focused at page call immediately and if you press the scr

相关标签:
2条回答
  • 2020-12-12 06:45

    Try using JQuery. On document ready you focus on the div. You make the div scrollable by setting the overflow property in css.

    JQuery:

    $(document).ready(function(){ $( "#scrollable_div" ).focus(); }
    

    CSS:

    #scrollable_div {
        overflow: scroll;
    }
    
    0 讨论(0)
  • 2020-12-12 07:01

    If you want to scroll the div even if the mouse is else where on the page try this plugin https://github.com/brandonaaron/jquery-mousewheel. Here is an example assuming your div has an id of content:

    $(function() {
        var $target = $('#content');
        $("body").mousewheel(function(event, delta) {
          $target.scrollTop($target.scrollTop() - (delta * 30));
          event.preventDefault();
       });
    });
    

    Quick fiddle: http://jsfiddle.net/5h47wygo/

    0 讨论(0)
提交回复
热议问题