简体   繁体   English

滚动条,不会干扰悬停时的文字换行

[英]Scrollbar that doesn't interfere with text-wrap on hover

I have the following structure on many of the pages on my blog: 在博客的许多页面上,我都有以下结构:

http://jsfiddle.net/gliu/E32Bh/ http://jsfiddle.net/gliu/E32Bh/

Basically, boxes with too much text are displayed without vertical scrollbar until the user hovers over them. 基本上,显示过多文本的框将不显示垂直滚动条,直到用户将鼠标悬停在它们上方。

There is a problem though. 不过有一个问题。 When the scrollbar appears it creates a margin of its own which causes the text to wrap differently. 滚动条出现时,将创建其自己的边距,从而导致文本换行。

I need a solution where the scrollbar will not have an effect on text wrapping. 我需要一种解决方案,其中滚动条不会对文本换行产生影响。 The user must still be able to scroll through the DIV. 用户仍然必须能够滚动浏览DIV。 I'd prefer not to see the scrollbar hidden, but if that's the only way to do it, then go for it. 我不希望隐藏滚动条,但是如果这是唯一的方法,那就继续吧。 If there's a pure CSS/HTML solution to this, that will be the best! 如果有一个纯CSS / HTML解决方案,那将是最好的!

Code (since StackOverflow insists upon it) 代码(因为StackOverflow坚持使用它)

<p>Notice how the text gets wrapped differently once you hover over the div:</p>
<div id="scroller">
   <div class="content">
        Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.

The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
    </div>
</div>

div#scroller {
    position: absolute;
    width: 75%;
    height: 50%;
    margin: 0;
    padding: 0;
    background: black;
    color: white;
    overflow: hidden;
    border: 3px solid gold;
}
div#scroller:hover {
    overflow-y: scroll; /* Display scrollbar on hover only */
}
div.content {
    padding: 1em;    
    background: transparent;
}

A quick way to fix this is to always display a scrollbar even if there is nothing to scroll. 解决此问题的一种快速方法是即使没有滚动内容,也始终显示滚动条。 This prevents content shifting left and right. 这样可以防止内容向左和向右移动。

CSS: overflow-y: scroll; CSS: overflow-y: scroll;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM