首页 > 解决方案 > 如何在 html 中单击(或激活)幻灯片时将背景图像添加到幻灯片中的导航项目符号

问题描述

我创建了一个带有自定义导航项目符号的幻灯片。每个子弹都有自己定制的圆形图像。现在的问题是我有每个子弹的背景图像,它应该在单击或激活子弹时出现。

我试图添加使用.active如下的 CSS 功能来更改背景图像,但它不起作用。

CSS

.bulletOne:active {
background-image: url("http://localhost/wordpress/wp-content/uploads/2020/06/CallOfDuty_2.png");
}

下面添加了我的完整 HTML 代码以及 css、html 和 js 供您参考,请帮助我。先感谢您

除了图像链接,其他一切都可以正常工作,您可以通过替换 w3schools 中的这些链接来测试它尝试平台

完整代码

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box}
body {font-family: Verdana, sans-serif; margin:0}
.mySlides {display: none}
img {vertical-align: middle;}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */


/* The dots/bullets/indicators */
.dotOne {
  cursor: pointer;
  height: 75px;
  width: 75px;
   padding: 8px 40px 20px;
  margin: 0 2px;
   background-image: url("http://localhost/wordpress/wp-content/uploads/2020/06/Cyberpunk-2077.png");
   border-radius: 50%;
  display: inline-block;
   transition:background-color :#ffffff; 
}
.dotTwo {
  cursor: pointer;
  height: 75px;
  width: 75px;
   padding: 8px 40px 20px;
  margin: 0 2px;
   background-image: url("http://localhost/wordpress/wp-content/uploads/2020/06/Sales_Icon_Cropped.png");
   border-radius: 50%;
  display: inline-block;
   transition:background-color :#ffffff; 
}
.dotThree {
  cursor: pointer;
  height: 75px;
  width: 75px;
   padding: 8px 40px 20px;
  margin: 0 2px;
   background-image: url("http://localhost/wordpress/wp-content/uploads/2020/06/CallOfDuty_2.png");
   border-radius: 50%;
  display: inline-block;
   transition:background-color :#ffffff; 
}
.dotFour {
  cursor: pointer;
  height: 75px;
  width: 75px;
   padding: 8px 40px 20px;
  margin: 0 2px;
   background-image: url("http://localhost/wordpress/wp-content/uploads/2020/06/Age-of-Empires-IV.png");
   border-radius: 50%;
  display: inline-block;
   transition:background-color :#ffffff; 
}

.dotFour:active {
  background-image: url("http://localhost/wordpress/wp-content/uploads/2020/06/CallOfDuty_2.png");
}


/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4} 
  to {opacity: 1}
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {
  .prev, .next,.text {font-size: 11px}
}
</style>
</head>
<body>
<div style="text-align:center">
  <span class="dotOne" onclick="currentSlide(1)"></span> 
  <span class="dotTwo" onclick="currentSlide(2)"></span> 
  <span class="dotThree" onclick="currentSlide(3)"></span> 
  <span class="dotFour" onclick="currentSlide(4)"></span> 
</div>

<div class="slideshow-container">

<div class="mySlides fade">

  <img src="http://localhost/wordpress/wp-content/uploads/2020/06/HellBlade_2.png" style="width:100%">

</div>

<div class="mySlides fade">

  <img src="http://localhost/wordpress/wp-content/uploads/2020/06/CallOfDuty_WarZone_PC.png" style="width:100%">

</div>

<div class="mySlides fade">

  <img src="http://localhost/wordpress/wp-content/uploads/2020/06/CallOfDuty_2.png" style="width:100%">

</div>

<div class="mySlides fade">

  <img src="http://localhost/wordpress/wp-content/uploads/2020/06/Age-of-Empires-IV.png" style="width:100%">

</div>

<a class="prev" onclick="plusSlides(-1)">&#10094;</a>
<a class="next" onclick="plusSlides(1)">&#10095;</a>

</div>
<br>



<script>
var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var dots;
  var slides = document.getElementsByClassName("mySlides");
  if(n==1){
  dots = document.getElementsByClassName("dotOne");
  }else if(n==2){
  dots = document.getElementsByClassName("dotTwo");
  }else if(n==3){
  dots = document.getElementsByClassName("dotThree");
  }else if(n==4){
  dots = document.getElementsByClassName("dotFour");
  }

  if (n > slides.length) {slideIndex = 1}    
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";  
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";  
  dots[slideIndex-1].className += " active";
}
</script>

</body>
</html> 

标签: javascripthtmlcsscustom-wordpress-pages

解决方案


有一些问题。

  1. 在脚本中,var dots应该是一个数组。我将该类添加dots到 html 中的点元素中,并在脚本中进行了所需的调整。
var dots = document.getElementsByClassName("dots");
  1. 在 css 中,您使用的是dots:active,但我不确定这是您想要的。

    • :active 选择器用于单击项目符号时发生的事件。

    • 使用自定义类.active来设置与当前显示的图像相对应的项目符号的样式。

这是编辑后的片段。我包括了:active,.active:hover可能性。只需将样式更改为您想要的样式即可。

:focus不适用于这里。您不能将焦点应用于所有 HTML 元素。如果您想了解更多信息,请阅读此内容。

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var dots = document.getElementsByClassName("dots");
  var slides = document.getElementsByClassName("mySlides");


  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex - 1].style.display = "block";
  dots[slideIndex - 1].className += " active";
}

function goToSlideshow(){
  var slideshow = document.getElementsByClassName("slideshow-container");
  slideshow[0].scrollIntoView({ behavior: 'smooth' });
}
* {
  box-sizing: border-box
}

body {
  font-family: Verdana, sans-serif;
  margin: 0
}

.mySlides {
  display: none
}

img {
  vertical-align: middle;
}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Next & previous buttons */
.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -22px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */


/* The dots/bullets/indicators */
.dotOne {
  cursor: pointer;
  height: 75px;
  width: 75px;
  padding: 8px 40px 20px;
  margin: 0 2px;
  background-image: url("https://images.unsplash.com/photo-1514530963096-e9aa1f285eb5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80");
  border-radius: 50%;
  display: inline-block;
  transition: background-color:#ffffff;
}

.dotTwo {
  cursor: pointer;
  height: 75px;
  width: 75px;
  padding: 8px 40px 20px;
  margin: 0 2px;
  background-image: url("https://images.unsplash.com/photo-1484447089447-a742155835e2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80");
  border-radius: 50%;
  display: inline-block;
  transition: background-color:#ffffff;
}

.dotThree {
  cursor: pointer;
  height: 75px;
  width: 75px;
  padding: 8px 40px 20px;
  margin: 0 2px;
  background-image: url("https://images.unsplash.com/photo-1504909297680-b34eb832a31f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80");
  border-radius: 50%;
  display: inline-block;
  transition: background-color:#ffffff;
}

.dotFour {
  cursor: pointer;
  height: 75px;
  width: 75px;
  padding: 8px 40px 20px;
  margin: 0 2px;
  background-image: url("https://images.unsplash.com/photo-1445023835378-9fa9a2089f0c?ixlib=rb-1.2.1&auto=format&fit=crop&w=667&q=80");
  border-radius: 50%;
  display: inline-block;
  transition: background-color:#ffffff;
}

.dots:hover{
  background: lightgreen;
}

.dots:active {
  background: red;
}

.dots.active {
  border: 2px solid red;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {
    opacity: .4
  }

  to {
    opacity: 1
  }
}

@keyframes fade {
  from {
    opacity: .4
  }

  to {
    opacity: 1
  }
}

/* On smaller screens, decrease text size */
@media only screen and (max-width: 300px) {

  .prev,
  .next,
  .text {
    font-size: 11px
  }
}
<body>
  <div style="text-align:center">
    <span class="dots dotOne" onclick="currentSlide(1); goToSlideshow();"></span>
    <span class="dots dotTwo" onclick="currentSlide(2); goToSlideshow();"></span>
    <span class="dots dotThree" onclick="currentSlide(3); goToSlideshow();"></span>
    <span class="dots dotFour" onclick="currentSlide(4); goToSlideshow();"></span>
  </div>

  <div class="slideshow-container">

    <div class="mySlides fade">

      <img src="https://images.unsplash.com/photo-1514530963096-e9aa1f285eb5?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80" style="width:100%">

    </div>

    <div class="mySlides fade">

      <img src="https://images.unsplash.com/photo-1484447089447-a742155835e2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80" style="width:100%">

    </div>

    <div class="mySlides fade">

      <img src="https://images.unsplash.com/photo-1504909297680-b34eb832a31f?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=750&q=80" style="width:100%">

    </div>

    <div class="mySlides fade">

      <img src="https://images.unsplash.com/photo-1445023835378-9fa9a2089f0c?ixlib=rb-1.2.1&auto=format&fit=crop&w=667&q=80" style="width:100%">

    </div>

    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>

  </div>
  <br>


</body>


推荐阅读