CSS 只有横幅滑块 - 图片超链接不起作用
CSS only banner slider - Image hyperlink not working
.zd-slider { position:relative; overflow:hidden; margin-top: 0px; }
.zd-slider img[id*="img"] {width:100%; position:absolute; left:0; top:0; opacity:0; -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
/* set the width/height of the slideshow */
.zd-slider img#blank {display:block; width:100%; visibility:hidden;}
/* the slideshow */
.zd-slider #img_01 {-webkit-animation:autoplay 9s linear infinite 0s; animation:autoplay 9s linear infinite 0s;}
.zd-slider #img_02 {-webkit-animation:autoplay 9s linear infinite 3s; animation:autoplay 9s linear infinite 3s;}
.zd-slider #img_03 {-webkit-animation:autoplay 9s linear infinite 6s; animation:autoplay 9s linear infinite 6s; }
@-webkit-keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
@keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
/* the slide timing indicator */
.zd-slider #slide {width:98px; height:5px; position:absolute; left:0; bottom:0; background:rgba(255,255,255,0.5);
-webkit-animation:slide 3s linear infinite; animation:slide linear 3s infinite;}
@keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
@-webkit-keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
<div class="zd-container" id="zd-collection2"> <div class="zd-slider">
<img id="blank" src="http://via.placeholder.com/1440x133" alt="" />
<img id="img_01" src="http://via.placeholder.com/1440x133" alt="" />
<a href="http://www.google.com">
<img id="img_02" src="http://via.placeholder.com/1440x133" alt="" /></a>
<img id="img_03" src="http://via.placeholder.com/1440x133" alt="" /> <div id="slide"></div> </div> </div>
我正在使用这种幻灯片格式在 eBay 商品详情中显示横幅。我想将幻灯片 link 放到商店的各个页面上,但是我尝试以传统方式添加 hyperlink(请参阅 "img_02"),但绝对没有成功滑动可点击。我在这里看到了类似的问题,并尝试了一些不同的 "working" 解决方案,但都没有成功。如果有人可以建议如何解决这个问题,我们将不胜感激。
尝试添加以下 css 属性
.zd-slider a {
display: inline-block;
}
因为 a
标签没有任何宽度或高度,可能无法正确解释你,试试这个,肯定会起作用。
问题来自您的position: absolute
。
它从正常流中删除所有图像。所以 <a/>
标签不会 "contain" 图像。
您可以尝试使用静态位置和 transform: translate
动画而不是 opacity
,这样图像就不需要堆叠。
似乎是一个 z-index 问题,而且因为你绝对定位你的图像,你的 link 将没有高度。
在下面,我将你的图像包裹在 span 中并定位它而不是图像(使用与锚点相同的 class 这样也可以定位)并为你的动画添加一些 z-index (代码中的注释)。
.zd-slider {
position: relative;
overflow: hidden;
margin-top: 0px;
}
.zd-slider .container { /* move this from image to new container span / link */
display: block;
width: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
.zd-slider img {
display: block;
width: 100%;
}
/* set the width/height of the slideshow */
.zd-slider #blank {
visibility: hidden;
pointer-events: none; /* I added this in case it appeared on top of the absolute positioned elements - it shouldn't but just to be safe */
}
/* the slideshow */
.zd-slider #img_01 {
-webkit-animation: autoplay 9s linear infinite 0s;
animation: autoplay 9s linear infinite 0s;
}
.zd-slider #img_02 {
-webkit-animation: autoplay 9s linear infinite 3s;
animation: autoplay 9s linear infinite 3s;
}
.zd-slider #img_03 {
-webkit-animation: autoplay 9s linear infinite 6s;
animation: autoplay 9s linear infinite 6s;
}
@-webkit-keyframes autoplay {
4%,
33% {
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
z-index: 2; /* add this to make sure visible item has a higher z-index that none visible items */
}
0%,
37%,
100% {
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
z-index: 1; /* add this to make sure visible item has a higher z-index that none visible items */
}
}
@keyframes autoplay {
4%,
33% {
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
z-index: 2; /* add this to make sure visible item has a higher z-index that none visible items */
}
0%,
37%,
100% {
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
] z-index: 1; /* add this to make sure visible item has a higher z-index that none visible items */
}
}
/* the slide timing indicator */
.zd-slider #slide {
width: 98px;
height: 5px;
position: absolute;
left: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.5);
-webkit-animation: slide 3s linear infinite;
animation: slide linear 3s infinite;
z-index: 3; /* make slider appear above images */
}
@keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
@-webkit-keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
<div class="zd-container" id="zd-collection2">
<div class="zd-slider">
<!-- sizing div doesn't need container class-->
<span id="blank"><img src="http://via.placeholder.com/1440x133" alt="" /></span>
<span class="container" id="img_01"><img src="http://via.placeholder.com/1440x133" alt="" /></span>
<a href="http://www.google.com" class="container" id="img_02"><img src="http://via.placeholder.com/1440x135" alt="" /></a>
<span class="container" id="img_03"><img src="http://via.placeholder.com/1440x133" alt="" /></span>
<div id="slide"></div>
</div>
</div>
这有效,需要将位置、宽度、高度和 z-index 全部应用到 link:
#img_02_link{
position: absolute;
top:0; left:0;
width: 100%;
height: 100%;
z-index:1;
}
.zd-slider { position:relative; overflow:hidden; margin-top: 0px; }
.zd-slider img[id*="img"] {width:100%; position:absolute; left:0; top:0; opacity:0; -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
/* set the width/height of the slideshow */
.zd-slider img#blank {display:block; width:100%; visibility:hidden;}
/* the slideshow */
.zd-slider #img_01 {-webkit-animation:autoplay 9s linear infinite 0s; animation:autoplay 9s linear infinite 0s;}
.zd-slider #img_02 {-webkit-animation:autoplay 9s linear infinite 3s; animation:autoplay 9s linear infinite 3s;}
.zd-slider #img_03 {-webkit-animation:autoplay 9s linear infinite 6s; animation:autoplay 9s linear infinite 6s; }
@-webkit-keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
@keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
/* the slide timing indicator */
.zd-slider #slide {width:98px; height:5px; position:absolute; left:0; bottom:0; background:rgba(255,255,255,0.5);
-webkit-animation:slide 3s linear infinite; animation:slide linear 3s infinite;}
@keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
@-webkit-keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
<div class="zd-container" id="zd-collection2"> <div class="zd-slider">
<img id="blank" src="http://via.placeholder.com/1440x133" alt="" />
<img id="img_01" src="http://via.placeholder.com/1440x133" alt="" />
<a id="img_02_link" href="http://www.google.com" target="_blank">
<img id="img_02" src="http://via.placeholder.com/1440x133" alt="" />
</a>
<img id="img_03" src="http://via.placeholder.com/1440x133" alt="" />
<div id="slide"></div>
</div>
</div>
为每个滑块图像创建一个单独的 div 并为该公共图像提供 Z-index class div...
试试这个解决方案:
演示:https://jsfiddle.net/yc07b60j/
.zd-slider {
position: relative;
overflow: hidden;
margin-top: 0px;
}
.zd-slider a {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.zd-slider img[id*="img"] {
width: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
/* set the width/height of the slideshow */
.zd-slider img#blank {
display: block;
width: 100%;
visibility: hidden;
}
/* the slideshow */
.zd-slider #img_01 {
-webkit-animation: autoplay 9s linear infinite 0s;
animation: autoplay 9s linear infinite 0s;
}
.zd-slider #img_02 {
-webkit-animation: autoplay 9s linear infinite 3s;
animation: autoplay 9s linear infinite 3s;
}
.zd-slider #img_03 {
-webkit-animation: autoplay 9s linear infinite 6s;
animation: autoplay 9s linear infinite 6s;
}
@-webkit-keyframes autoplay {
4%,
33% {
z-index: 2;
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
}
0%,
37%,
100% {
z-index: 1;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
}
@keyframes autoplay {
4%,
33% {
z-index: 2;
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
}
0%,
37%,
100% {
z-index: 1;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
}
/* the slide timing indicator */
.zd-slider #slide {
width: 98px;
height: 5px;
position: absolute;
left: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.5);
-webkit-animation: slide 3s linear infinite;
animation: slide linear 3s infinite;
}
@keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
@-webkit-keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
<div class="zd-container" id="zd-collection2">
<div class="zd-slider">
<img id="blank" src="http://via.placeholder.com/1440x133/f44242/ffffff" alt="" />
<img id="img_01" src="http://via.placeholder.com/1440x133/86f441/ffffff" alt="" />
<a href="http://www.google.com">
<img id="img_02" src="http://via.placeholder.com/1440x133/f44242/ffffff" alt="" />
</a>
<img id="img_03" src="http://via.placeholder.com/1440x133/41aff4/ffffff" alt="" />
</div>
</div>
.zd-slider { position:relative; overflow:hidden; margin-top: 0px; }
.zd-slider img[id*="img"] {width:100%; position:absolute; left:0; top:0; opacity:0; -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
/* set the width/height of the slideshow */
.zd-slider img#blank {display:block; width:100%; visibility:hidden;}
/* the slideshow */
.zd-slider #img_01 {-webkit-animation:autoplay 9s linear infinite 0s; animation:autoplay 9s linear infinite 0s;}
.zd-slider #img_02 {-webkit-animation:autoplay 9s linear infinite 3s; animation:autoplay 9s linear infinite 3s;}
.zd-slider #img_03 {-webkit-animation:autoplay 9s linear infinite 6s; animation:autoplay 9s linear infinite 6s; }
@-webkit-keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
@keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
/* the slide timing indicator */
.zd-slider #slide {width:98px; height:5px; position:absolute; left:0; bottom:0; background:rgba(255,255,255,0.5);
-webkit-animation:slide 3s linear infinite; animation:slide linear 3s infinite;}
@keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
@-webkit-keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
<div class="zd-container" id="zd-collection2"> <div class="zd-slider">
<img id="blank" src="http://via.placeholder.com/1440x133" alt="" />
<img id="img_01" src="http://via.placeholder.com/1440x133" alt="" />
<a href="http://www.google.com">
<img id="img_02" src="http://via.placeholder.com/1440x133" alt="" /></a>
<img id="img_03" src="http://via.placeholder.com/1440x133" alt="" /> <div id="slide"></div> </div> </div>
我正在使用这种幻灯片格式在 eBay 商品详情中显示横幅。我想将幻灯片 link 放到商店的各个页面上,但是我尝试以传统方式添加 hyperlink(请参阅 "img_02"),但绝对没有成功滑动可点击。我在这里看到了类似的问题,并尝试了一些不同的 "working" 解决方案,但都没有成功。如果有人可以建议如何解决这个问题,我们将不胜感激。
尝试添加以下 css 属性
.zd-slider a {
display: inline-block;
}
因为 a
标签没有任何宽度或高度,可能无法正确解释你,试试这个,肯定会起作用。
问题来自您的position: absolute
。
它从正常流中删除所有图像。所以 <a/>
标签不会 "contain" 图像。
您可以尝试使用静态位置和 transform: translate
动画而不是 opacity
,这样图像就不需要堆叠。
似乎是一个 z-index 问题,而且因为你绝对定位你的图像,你的 link 将没有高度。
在下面,我将你的图像包裹在 span 中并定位它而不是图像(使用与锚点相同的 class 这样也可以定位)并为你的动画添加一些 z-index (代码中的注释)。
.zd-slider {
position: relative;
overflow: hidden;
margin-top: 0px;
}
.zd-slider .container { /* move this from image to new container span / link */
display: block;
width: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
.zd-slider img {
display: block;
width: 100%;
}
/* set the width/height of the slideshow */
.zd-slider #blank {
visibility: hidden;
pointer-events: none; /* I added this in case it appeared on top of the absolute positioned elements - it shouldn't but just to be safe */
}
/* the slideshow */
.zd-slider #img_01 {
-webkit-animation: autoplay 9s linear infinite 0s;
animation: autoplay 9s linear infinite 0s;
}
.zd-slider #img_02 {
-webkit-animation: autoplay 9s linear infinite 3s;
animation: autoplay 9s linear infinite 3s;
}
.zd-slider #img_03 {
-webkit-animation: autoplay 9s linear infinite 6s;
animation: autoplay 9s linear infinite 6s;
}
@-webkit-keyframes autoplay {
4%,
33% {
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
z-index: 2; /* add this to make sure visible item has a higher z-index that none visible items */
}
0%,
37%,
100% {
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
z-index: 1; /* add this to make sure visible item has a higher z-index that none visible items */
}
}
@keyframes autoplay {
4%,
33% {
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
z-index: 2; /* add this to make sure visible item has a higher z-index that none visible items */
}
0%,
37%,
100% {
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
] z-index: 1; /* add this to make sure visible item has a higher z-index that none visible items */
}
}
/* the slide timing indicator */
.zd-slider #slide {
width: 98px;
height: 5px;
position: absolute;
left: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.5);
-webkit-animation: slide 3s linear infinite;
animation: slide linear 3s infinite;
z-index: 3; /* make slider appear above images */
}
@keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
@-webkit-keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
<div class="zd-container" id="zd-collection2">
<div class="zd-slider">
<!-- sizing div doesn't need container class-->
<span id="blank"><img src="http://via.placeholder.com/1440x133" alt="" /></span>
<span class="container" id="img_01"><img src="http://via.placeholder.com/1440x133" alt="" /></span>
<a href="http://www.google.com" class="container" id="img_02"><img src="http://via.placeholder.com/1440x135" alt="" /></a>
<span class="container" id="img_03"><img src="http://via.placeholder.com/1440x133" alt="" /></span>
<div id="slide"></div>
</div>
</div>
这有效,需要将位置、宽度、高度和 z-index 全部应用到 link:
#img_02_link{
position: absolute;
top:0; left:0;
width: 100%;
height: 100%;
z-index:1;
}
.zd-slider { position:relative; overflow:hidden; margin-top: 0px; }
.zd-slider img[id*="img"] {width:100%; position:absolute; left:0; top:0; opacity:0; -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
/* set the width/height of the slideshow */
.zd-slider img#blank {display:block; width:100%; visibility:hidden;}
/* the slideshow */
.zd-slider #img_01 {-webkit-animation:autoplay 9s linear infinite 0s; animation:autoplay 9s linear infinite 0s;}
.zd-slider #img_02 {-webkit-animation:autoplay 9s linear infinite 3s; animation:autoplay 9s linear infinite 3s;}
.zd-slider #img_03 {-webkit-animation:autoplay 9s linear infinite 6s; animation:autoplay 9s linear infinite 6s; }
@-webkit-keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
@keyframes autoplay {
4%,33% {opacity:1;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";}
0%,37%,100% {opacity:0;-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";}
}
/* the slide timing indicator */
.zd-slider #slide {width:98px; height:5px; position:absolute; left:0; bottom:0; background:rgba(255,255,255,0.5);
-webkit-animation:slide 3s linear infinite; animation:slide linear 3s infinite;}
@keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
@-webkit-keyframes slide {
0% {left:-100px;}
100% {left:100%;}
}
<div class="zd-container" id="zd-collection2"> <div class="zd-slider">
<img id="blank" src="http://via.placeholder.com/1440x133" alt="" />
<img id="img_01" src="http://via.placeholder.com/1440x133" alt="" />
<a id="img_02_link" href="http://www.google.com" target="_blank">
<img id="img_02" src="http://via.placeholder.com/1440x133" alt="" />
</a>
<img id="img_03" src="http://via.placeholder.com/1440x133" alt="" />
<div id="slide"></div>
</div>
</div>
为每个滑块图像创建一个单独的 div 并为该公共图像提供 Z-index class div...
试试这个解决方案:
演示:https://jsfiddle.net/yc07b60j/
.zd-slider {
position: relative;
overflow: hidden;
margin-top: 0px;
}
.zd-slider a {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.zd-slider img[id*="img"] {
width: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
/* set the width/height of the slideshow */
.zd-slider img#blank {
display: block;
width: 100%;
visibility: hidden;
}
/* the slideshow */
.zd-slider #img_01 {
-webkit-animation: autoplay 9s linear infinite 0s;
animation: autoplay 9s linear infinite 0s;
}
.zd-slider #img_02 {
-webkit-animation: autoplay 9s linear infinite 3s;
animation: autoplay 9s linear infinite 3s;
}
.zd-slider #img_03 {
-webkit-animation: autoplay 9s linear infinite 6s;
animation: autoplay 9s linear infinite 6s;
}
@-webkit-keyframes autoplay {
4%,
33% {
z-index: 2;
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
}
0%,
37%,
100% {
z-index: 1;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
}
@keyframes autoplay {
4%,
33% {
z-index: 2;
opacity: 1;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
}
0%,
37%,
100% {
z-index: 1;
opacity: 0;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
}
}
/* the slide timing indicator */
.zd-slider #slide {
width: 98px;
height: 5px;
position: absolute;
left: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.5);
-webkit-animation: slide 3s linear infinite;
animation: slide linear 3s infinite;
}
@keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
@-webkit-keyframes slide {
0% {
left: -100px;
}
100% {
left: 100%;
}
}
<div class="zd-container" id="zd-collection2">
<div class="zd-slider">
<img id="blank" src="http://via.placeholder.com/1440x133/f44242/ffffff" alt="" />
<img id="img_01" src="http://via.placeholder.com/1440x133/86f441/ffffff" alt="" />
<a href="http://www.google.com">
<img id="img_02" src="http://via.placeholder.com/1440x133/f44242/ffffff" alt="" />
</a>
<img id="img_03" src="http://via.placeholder.com/1440x133/41aff4/ffffff" alt="" />
</div>
</div>