如何修改轮播图代码实现右键保存当前显示图片?
问题
右键点击轮播图(carousel)中的图片并选择“保存”时,下载的是上一张图片而非当前显示的图片,需要修改代码中的哪部分才能解决?
当前使用的轮播代码如下:
CSS代码
.carousel { position: relative; box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.64); margin-top: 26px; } .carousel-inner { position: relative; overflow: hidden; width: 100%; } .carousel-open:checked+.carousel-item { position: static; opacity: 100; } .carousel-item { position: absolute; opacity: 0; -webkit-transition: opacity 0.6s ease-out; transition: opacity 0.6s ease-out; } .carousel-item img { display: block; height: auto; max-width: 100%; } .carousel-control { background: rgba(0, 0, 0, 0.28); border-radius: 50%; color: #fff; cursor: pointer; display: none; font-size: 40px; height: 40px; line-height: 35px; position: absolute; top: 50%; -webkit-transform: translate(0, -50%); cursor: pointer; -ms-transform: translate(0, -50%); transform: translate(0, -50%); text-align: center; width: 40px; z-index: 10; } .carousel-control.prev { left: 2%; } .carousel-control.next { right: 2%; } .carousel-control:hover { background: rgba(0, 0, 0, 0.8); color: #aaaaaa; } #carousel-1:checked~.control-1, #carousel-2:checked~.control-2, #carousel-3:checked~.control-3 { display: block; } .carousel-indicators { list-style: none; margin: 0; padding: 0; position: absolute; bottom: 2%; left: 0; right: 0; text-align: center; z-index: 10; } .carousel-indicators li { display: inline-block; margin: 0 5px; } .carousel-bullet { color: #fff; cursor: pointer; display: block; font-size: 35px; } .carousel-bullet:hover { color: #aaaaaa; } #carousel-1:checked~.control-1~.carousel-indicators li:nth-child(1) .carousel-bullet, #carousel-2:checked~.control-2~.carousel-indicators li:nth-child(2) .carousel-bullet, #carousel-3:checked~.control-3~.carousel-indicators li:nth-child(3) .carousel-bullet { color: #428bca; }
HTML代码
<div class="carousel"> <div class="carousel-inner"> <input class="carousel-open" type="radio" id="carousel-1" name="carousel" aria-hidden="true" hidden="" checked=""> <div class="carousel-item"> <img src="http://fakeimg.pl/2000x800/F90/fff/?text=TEST1"> </div> <input class="carousel-open" type="radio" id="carousel-2" name="carousel" aria-hidden="true" hidden=""> <div class="carousel-item"> <img src="http://fakeimg.pl/2000x800/F90/fff/?text=TEST2"> </div> <input class="carousel-open" type="radio" id="carousel-3" name="carousel" aria-hidden="true" hidden=""> <div class="carousel-item"> <img src="http://fakeimg.pl/2000x800/F90/fff/?text=TEST3"> </div> <label for="carousel-2" class="carousel-control next control-1">›</label> <label for="carousel-1" class="carousel-control prev control-2">‹</label> <label for="carousel-3" class="carousel-control next control-2">›</label> <label for="carousel-2" class="carousel-control prev control-3">‹</label> <ol class="carousel-indicators"> <li> <label for="carousel-1" class="carousel-bullet">•</label> </li> <li> <label for="carousel-2" class="carousel-bullet">•</label> </li> <li> <label for="carousel-3" class="carousel-bullet">•</label> </li> </ol> </div> </div>
原因分析
当前轮播的实现逻辑是:所有.carousel-item默认设为绝对定位且透明(opacity:0),只有被选中的项会通过position:static和opacity:100显示。但未选中的透明图片依然会叠在当前显示图片的上方——因为绝对定位元素的层级高于静态定位元素,且HTML中后定义的元素层级更高。右键点击时实际触发的是上层的透明图片,导致保存的不是当前显示的那张。
修改方案
方案1:提升当前显示项的层级
修改CSS中.carousel-open:checked+.carousel-item的样式,添加z-index:1,让当前显示的图片处于最上层:
.carousel-open:checked+.carousel-item { position: static; opacity: 100; z-index: 1; /* 添加这一行 */ }
方案2:禁止未选中项响应点击事件
给所有未选中的.carousel-item添加pointer-events:none,让它们无法接收鼠标事件,仅允许当前显示的项响应点击:
.carousel-item { position: absolute; opacity: 0; -webkit-transition: opacity 0.6s ease-out; transition: opacity 0.6s ease-out; pointer-events: none; /* 添加这一行 */ } .carousel-open:checked+.carousel-item { position: static; opacity: 100; pointer-events: auto; /* 添加这一行 */ }
两种方案都能解决问题,方案2更直接,从根源上阻止了透明图片接收点击事件,避免了层级相关的潜在问题。
内容的提问来源于stack exchange,提问作者Deniszin
相关产品推荐
相关产品推荐

