Make scrollbars only visible when a Div is hovered over?

后端 未结 10 1932
庸人自扰
庸人自扰 2020-12-02 15:04

I am trying to figure out how to have a scrollable div that only shows its scrollbars when Hovered.

Example is Google Image search, in the image below you can see

相关标签:
10条回答
  • 2020-12-02 15:53

    Give the div a fixed height and srcoll:hidden; and on hover change the scroll to auto;

    #test_scroll{ height:300px; overflow:hidden;}
    #test_scroll:hover{overflow-y:auto;}
    

    Here is an example. http://jsfiddle.net/Lywpk/

    0 讨论(0)
  • 2020-12-02 15:55

    The answer with changing overflow have a bunch of issues, like inconsistent width of the inner block and triggering of reflow.

    There is an easier way to have the same effect that would not trigger reflow ever: using visibility property and nested blocks:

    .scrollbox {
      width: 10em;
      height: 10em;
      overflow: auto;
      visibility: hidden;
    }
    
    .scrollbox-content,
    .scrollbox:hover,
    .scrollbox:focus {
      visibility: visible;
    }
    
    .scrollbox_delayed {
      transition: visibility 0.2s;
    }
    
    .scrollbox_delayed:hover {
      transition: visibility 0s 0.2s;
    }
    <h2>Hover it</h2>
    <div class="scrollbox" tabindex="0">
      <div class="scrollbox-content">Hover me! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere velit, repellat voluptas ipsa impedit fugiat voluptatibus. Facilis deleniti, nihil voluptate perspiciatis iure adipisci magni, nisi suscipit aliquam, quam, et excepturi! Lorem
        ipsum dolor sit amet, consectetur adipisicing elit. Facere velit, repellat voluptas ipsa impedit fugiat voluptatibus. Facilis deleniti, nihil voluptate perspiciatis iure adipisci magni, nisi suscipit aliquam, quam, et excepturi!</div>
    </div>
    
    <h2>With delay</h2>
    <div class="scrollbox scrollbox_delayed" tabindex="0">
      <div class="scrollbox-content">Hover me! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Facere velit, repellat voluptas ipsa impedit fugiat voluptatibus. Facilis deleniti, nihil voluptate perspiciatis iure adipisci magni, nisi suscipit aliquam, quam, et excepturi! Lorem
        ipsum dolor sit amet, consectetur adipisicing elit. Facere velit, repellat voluptas ipsa impedit fugiat voluptatibus. Facilis deleniti, nihil voluptate perspiciatis iure adipisci magni, nisi suscipit aliquam, quam, et excepturi!</div>
    </div>

    Another feature of this method is that visibility is animatable, so we can add a transition to it (see the second example in the pen above). Adding a transition would be better for UX: the scrollbar won't appear immediately when hovered just while moving along to another element, and it would be harder to miss the scrollbar when targeting it with mouse cursor, as it won't hide immediately as well.

    0 讨论(0)
  • 2020-12-02 15:58

    If you are only concern about showing/hiding, this code would work just fine:

    $("#leftDiv").hover(function(){$(this).css("overflow","scroll");},function(){$(this).css("overflow","hidden");});
    

    However, it might modify some elements in your design, in case you are using width=100%, considering that when you hide the scrollbar, it creates a little bit of more room for your width.

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

    This will work:

    #div{
         max-height:300px;
         overflow:hidden;
    }
    #div:hover{
         overflow-y:scroll;
    }
    
    0 讨论(0)
提交回复
热议问题