简体   繁体   English

JS:工具提示在图标右侧的相对定位

[英]JS: Relative positioning of tooltip on right of icon

I have an icon, and when you hover over it, I would like to have a custom CSS tooltip appear to the right of the icon.我有一个图标,当您将鼠标悬停在它上面时,我希望在图标右侧显示一个自定义 CSS 工具提示。 Whether or not you scroll up or down the page, the tooltip will always need to appear to the right of the icon.无论您是向上还是向下滚动页面,工具提示都需要始终显示在图标的右侧。

And no, I don't want to use any plugins.不,我不想使用任何插件。 I just want a little JS/CSS to get the job done.我只想要一点 JS/CSS 来完成工作。 If you use JQuery, it needs to be compatible with v1.7, and JQuery-UI: v1.8.如果你使用JQuery,它需要兼容v1.7,和JQuery-UI:v1.8。

In addition, it needs to be compatible with IE 6 and 7 .此外,它需要与 IE 6 和 7 兼容

I would prefer to leave my elements as siblings, but it looks like under certain circumstances the div that appears needs to be a child element, so it's OK if the HTML needs to be changed.我更愿意将我的元素保留为兄弟元素,但看起来在某些情况下出现的 div 需要是子元素,因此如果需要更改 HTML 也没关系。

HTML: HTML:

<img src="" class="icon">ICON<img/>

<div class="demo">
    STUFF<br/>
    STUFF<br/>
    STUFF<br/>
    STUFF<br/>
    STUFF<br/>
</div>

CSS: CSS:

.demo {
    margin-left: 5px;
    padding: 10px;
    width: 265px;
    height: 110px;
    background-color: #ccc;
    position: relative;
    border: 2px solid #333;
}

.demo:after, .demo:before {
    border: solid transparent;
    content: ' ';
    height: 0;
    right: 100%;
    position: absolute;
    width: 0;
}

.demo:after {
    border-width: 11px;
    border-right-color: #ccc;
    top: 13px;
}

.demo:before {
    border-width: 14px;
    border-right-color: #333;
    top: 10px;
}

Live Example: http://jsfiddle.net/49Js3/16/现场示例: http : //jsfiddle.net/49Js3/16/

Since my reputation isn't high enough to comment on the answer above, I just wanted to add an updated fiddle (based on the above answer) that positions the tooltip absolutely, but with display: inline-block so that it is not fixed to certain position from the left and will show to the right: here is the important bit:由于我的声誉不够高,无法对上述答案发表评论,因此我只想添加一个更新的小提琴(基于上述答案),绝对定位工具提示,但使用 display: inline-block 这样它就不会固定到左侧的某个位置并将显示在右侧:这是重要的一点:

a.tippy:hover + div {
    display: inline-block;
    position: absolute;
}

http://jsfiddle.net/7gmv3wo2/ http://jsfiddle.net/7gmv3wo2/

Fiddle here: http://jsfiddle.net/49Js3/29/在这里小提琴: http : //jsfiddle.net/49Js3/29/

I don't have access to IE6, so I don't know whether it's legal.我无法访问IE6,所以我不知道它是否合法。 I do know you'll need an anchor to get hover behavior with CSS in IE7 and earlier.我知道您需要一个锚点才能在 IE7 及更早版本中使用 CSS 获得悬停行为。

So, I added an anchor around your image, as well as a div to contain the tooltip.因此,我在您的图像周围添加了一个锚点,以及一个包含工具提示的 div。

HTML HTML

<div class="outer">
<a class="tippy" href="">
        <img src="" class="icon">ICON<img/>
   </a>

    <div class="demo">STUFF
        <br/>STUFF
        <br/>STUFF
        <br/>STUFF
        <br/>STUFF
        <br/>
    </div>
</div>

And here is the CSS:这是CSS:

.tippy {
    text-decoration: none;
}
.outer {
    width: 350px;
}
a.tippy:hover + div {
    display:block;
    float: right;
}
.demo {
    margin-left: 5px;
    padding: 10px;
    width: 265px;
    height: 110px;
    background-color: #ccc;
    position: relative;
    border: 2px solid #333;
    display: none;
}
.demo:after, .demo:before {
    border: solid transparent;
    content:' ';
    height: 0;
    right: 100%;
    position: absolute;
    width: 0;
}
.demo:after {
    border-width: 11px;
    border-right-color: #ccc;
    top: 13px;
}
.demo:before {
    border-width: 14px;
    border-right-color: #333;
    top: 10px;
}

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

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