简体   繁体   English

背景图像出现在链接悬停上

[英]Background image appear on link hover

I am trying to have a number of links within a single paragraph that each display a different image on link hover.我试图在单个段落中包含多个链接,每个链接在链接悬停时显示不同的图像。 I want the image to appear behind the text and disappear when mouse moves off link but I have been stuck for a while我希望图像出现在文本后面并在鼠标移出链接时消失,但我已经卡住了一段时间

I want the images to appear top left on hover我希望图像在悬停时出现在左上角

https://codepen.io/anon/pen/yZLvXW https://codepen.io/anon/pen/yZLvXW

Happy to use jQuery or css, any suggestions much appreciated很高兴使用 jQuery 或 css,非常感谢任何建议

span:hover {
background-image: 
url("https://i.ytimg.com/vi/91v7TX1PcWU/hqdefault.jpg");
position: absolute;
left: 0;
}

Remove position: absolute;移除position: absolute; and left: 0; left: 0; also add background-size: 100% 100%;还添加background-size: 100% 100%; . .

Do in same way to other links以同样的方式处理其他链接

 a { text-decoration: none; color:#000; } a:hover { color: red; } span:hover { background-image: url("https://i.ytimg.com/vi/91v7TX1PcWU/hqdefault.jpg"); background-size: 100% 100%; }
 <h1> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text <span class="link1"><a href="#">(1) link#1</a></span> <a href="#">(2) link#2</a> <a href="#">(3) link#3</a> text text text text text text text text text text </h1>

EDIT编辑

I want the images to appear top left on hover我希望图像在悬停时出现在左上角

Set position to img to top left of pageposition设置为img页面左上角

 a { text-decoration: none; color:#000; } a:hover { color: red; } span:hover a{ color:red; } span:hover img { display:block; } img{ width: 50px; position: absolute; top: 0; left: 0; margin-bottom: 15px; display:none; }
 <h1> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text <span class="link1"> <a href="#">(1) link#1</a> <img src="https://i.ytimg.com/vi/91v7TX1PcWU/hqdefault.jpg"/> </span> <a href="#">(2) link#2</a> <a href="#">(3) link#3</a> text text text text text text text text text text </h1>

Set position to img to top left of spanposition设置为imgspan左上角

 a { text-decoration: none; color:#000; } a:hover { color: red; } span{ position:relative; } span:hover a{ color:red; } span:hover img { display:block; } img{ width: 20px; position: absolute; top: 0; left: 0; z-index: -1; display:none; }
 <h1> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text <span class="link1"> <a href="#">(1) link#1</a> <img src="https://i.ytimg.com/vi/91v7TX1PcWU/hqdefault.jpg"/> </span> <a href="#">(2) link#2</a> <a href="#">(3) link#3</a> text text text text text text text text text text </h1>

Less code and much funtionalty更少的代码和更多的功能

Just add a class to the <a> tags and define the style in css.只需在<a>标签中添加一个class并在 css 中定义样式。

There is no need for extra elements and classes for your goal.您的目标不需要额外的元素和类。

This is the result: https://codepen.io/anon/pen/rPNJbe这是结果: https : //codepen.io/anon/pen/rPNJbe

It can be simple as this: Making the 'span' tag a block element.它可以很简单:使“span”标签成为块元素。

span {
  display: inline-block;
}

span:hover {
  background-image: url("https://i.ytimg.com/vi/91v7TX1PcWU/hqdefault.jpg"); 
}

Solution: https://codepen.io/anon/pen/pGoaQz解决方案: https : //codepen.io/anon/pen/pGoaQz

You can use before tag to insert the image on hover state.您可以使用before标签在hover状态插入图像。 This way you not need to change you entire code.这样您就不需要更改整个代码。

See the comment line // changes here .请参阅注释行// changes here I changed span:hover to span:hover:before and put content , height and width .我将span:hover更改为span:hover:before并放置contentheightwidth After that, to show image in top left and behind the text, I use top , left and z-index .之后,为了在左上角和文本后面显示图像,我使用topleftz-index You need to change height and width with the image size.您需要根据图像大小更改heightwidth

See working here: https://codepen.io/anon/pen/daymOK请参阅此处的工作: https : //codepen.io/anon/pen/daymOK

 a { text-decoration: none; color:#000; } a:hover { color: red; } span { } // Changes here span:hover:before { content: ''; background-image: url("https://i.ytimg.com/vi/91v7TX1PcWU/hqdefault.jpg"); position: absolute; left: 0; top: 0; width: 10%; height: 10%; z-index: -1; }
 <h1> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text <span class="link1"><a href="#">(1) link#1</a></span> <a href="#">(2) link#2</a> <a href="#">(3) link#3</a> text text text text text text text text text text </h1>

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

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