为什么我的css for span悬停不起作用?

时间:2015-12-11 01:44:49

标签: html css

当您将鼠标移到图像缩略图上时,即ul .thumbs中的所有图像时,您应该会看到一个小框,其中显示图像链接中嵌入的范围中的文本。这不会发生。为什么以及如何解决?

http://jsfiddle.net/raj4dev/hbyg43d9/3/

HTML

<body>
    <div id="container">
        <h1>css slide show</h1>
        <ul class="thumbs">
            <li><a href="#slide-1"><img src="img/thumb1.jpg"><span>Img 1</span></a></li>
            <li><a href="#slide-2"><img src="img/thumb2.jpg"><span>Img 2</span></a></li>
            <li><a href="#slide-3"><img src="img/thumb3.jpg"><span>Img 3</span></a></li>
        </ul>

        <ul class="slides">
            <li class="first" id="slide-1"><img src="img/slide1.jpg"></li>
            <li id="slide-2"><img src="img/slide2.jpg"></li>
            <li id="slide-3"><img src="img/slide3.jpg"></li>
        </ul>

    </div>
</body>

CSS

*{
    margin: 0;
    padding: 0;
    border: none;
    outline: none;
    list-style: none;
}

body{
    background: #465c8f url(../img/bg-image.jpg) repeat-x;
    font-family: 'Arial', 'sans-serif';
}

#container{
    width: 718px;
    overflow: hidden;
    margin: 40px auto;
}

h1{
    color: #fff;
    text-align: center;
    margin-bottom: 20px;
}

ul.thumbs li{
    float: left;
    margin-bottom: 10px;
    margin-right: 9px;
}

ul.thumbs a{
    display: block;
    position: relative;
    width: 85px;
    height: 55px;
    border: 4px solid transparent;
    font: bold 12px/25px Arial, sans-serif;
    color: #515151;
    text-decoration: none;/*remove underlines*/
    text-shadow: 1px 1px 0px rgba(255,255,255,0.25), inset 1px 1px 0px rgba(0,0,0,0.15);
}

ul.thumbs img{
    border: #333 solid 4px;
}

ul.slide{
    overflow: hidden;
    clear: both;
    border: #333 solid 4px;
}

ul.slides, ul.slides li, ul.slides a, ul.slides img{
    width: 705;
    height: 350px;
    position: relative;
}

ul.thumbs li a:hover span{
    position: absolute;
    z-index: 101;
    bottom: -30px;
    left: -22px;
    display: block;
    width: 100px;
    height: 25px;
    text-align: center;
    border-radius: 3px;
}

3 个答案:

答案 0 :(得分:1)

这是一种创建不需要JavaScript或jQuery的幻灯片的巧妙方法,而且非常好。

CSS中的某个类名中存在拼写错误,这造成了一些混淆(将ul.slide更改为ul.slides)。

我猜想你想要做的是在悬停时显示span,这意味着首先需要使用span隐藏display: none,我添加了一个ul.thumbs li a span的新CSS规则与ul.thumbs li a:hover span对应。 (注意,您也可以使用:hover上的li来获得类似效果。)

我还改变了浮动元素的样式。如果将overflow: auto添加到ul.thumbs,则所有浮点数都包含在父块中,然后您可以将底部边距添加到父ul而不是li,其中在某些设计中更有利,你可以决定。

对于缩略图,请参阅ul.thumbs img,我将高度设置为100%并让缩略图缩放以适​​合继承的高度(来自li)并根据需要使用vertical-align: top删除图像下方的空白区域。

我还在li而不是a上设置了with,但区别实际上取决于我们设计的细节。

在大多数情况下,你的CSS很好。唯一缺少的概念是span的初始隐藏,以便它可以在悬停时出现。

注意:我没注意span的宽度及其确切位置。如果你有很多文本(如标题),100%的宽度是不够的(我设置它以使其适合li容器)。您可以根据需要进行更改。

&#13;
&#13;
*{
	margin: 0;
	padding: 0;
	border: none;
	outline: none;
	list-style: none;
}

body {
	background: #465c8f url(../img/bg-image.jpg) repeat-x;
	font-family: 'Arial', 'sans-serif';
}

#container{
	width: 718px;
	overflow: hidden;
	margin: 40px auto;
}

h1{
	color: #fff;
	text-align: center;
	margin-bottom: 20px;
}

ul.thumbs {
    border: 1px dotted white; /* for demo only... */
    overflow: auto;
	margin-bottom: 10px;
}

ul.thumbs li{
	float: left;
	width: 85px;
	height: auto;
    margin-right: 9px;
    border: 1px dotted white; /* for demo only... */
}

ul.thumbs a {
	display: block;
	position: relative;
	border: 4px solid transparent;
	font: bold 12px/25px Arial, sans-serif;
	color: #515151;
	text-decoration: none;/*remove underlines*/
	text-shadow: 1px 1px 0px rgba(255,255,255,0.25), inset 1px 1px 0px rgba(0,0,0,0.15);
}

ul.thumbs img{
    vertical-align: top; /* if you need to remove whitespace below image */
    height: 100%;
	border: #333 solid 4px;
}

ul.slides {  /* fix typo in class name */
	overflow: hidden;
	clear: both;
	border: #333 solid 4px;
}

ul.slides, ul.slides li, ul.slides a, ul.slides img{
	width: 705;
	height: 350px;
	position: relative;
}

ul.thumbs li a span { /* Need to provide a default styling for the span... */
	position: absolute;
	bottom: 0px;
	left: 0px;
	display: block;
	width: 100%;
	height: auto;
	text-align: center;
	border-radius: 3px;
    background-color: white;
    display: none;
}

ul.thumbs li a:hover span {
    display: block;
}
&#13;
<div id="container">
  <h1>css slide show</h1>
  
  <ul class="thumbs">
    <li><a href="#slide-1"><img src="http://placehold.it/60x60"><span>Img 1</span></a></li>
    <li><a href="#slide-2"><img src="http://placehold.it/60x60"><span>Img 2</span></a></li>
    <li><a href="#slide-3"><img src="http://placehold.it/60x60"><span>Img 3</span></a></li>
  </ul>

  <ul class="slides">
    <li class="first" id="slide-1"><img src="http://placehold.it/240x120"></li>
    <li id="slide-2"><img src="http://placehold.it/180x120"></li>
    <li id="slide-3"><img src="http://placehold.it/120x120"></li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您的悬停样式工作正常,但您ul.slides之上ul.thumbs:hover操作未被传递给您的主播。

将来,请在StackOverflow上分享您的问题中的相关代码,以便后人和可搜索。

答案 2 :(得分:0)

只需添加z-index:2;对你的ul.thumbs来说,像coryward这样的css说你的链接位于某个东西之下,所以你不能将它悬停在它上面你需要将它带到顶部,这样你就可以将它悬停在它上面。

相关问题