0

I have created a slideshow with custom navigation bullets. Each bullets have its own customized rounded images.Now the problem is I have background images for each bullets and it should appear when bullets has been clicked or active.

I have tried to add using .active css functionality like below to change background image but it is not working.

CSS

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

My full HTML code has been added below for your reference along with css, html and js Please help me. Thank you in advance

Except image link everything else work fine you could test by replacing those links in w3schools try it platform

Full code

<!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> 
Andre Nevares
  • 711
  • 6
  • 21

1 Answers1

0

There were a few problems.

  1. In the script, var dots should be an array. I added the class dots to your dot elements in the html and made the adjustments needed in the script.
var dots = document.getElementsByClassName("dots");
  1. In the css, you were using dots:active, but I'm not sure it's what you want here.

    • Use the :active selector for events happening when the bullet is clicked.

    • Use a custom class like .active for styling the bullet corresponding to the image currently displayed.

Here is the edited snippet. I included the :active, .active and :hover possibilities. Just change the styling to what you want.

:focus doesn't apply here. You can not apply focus to all HTML element. Read this if you want to know more.

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>
tamarin
  • 549
  • 4
  • 12
  • Thank you Tamarin I have got this result and added background url to each navigation bullets. Now the problem is by default dots dotOne is on active status. I want to change to red only when it is clicked. Let me add css below – RH_Negative Jun 08 '20 at 00:36
  • dotOne.active{ background: green; } Like wise for each Navigation bullets different color should appear. How to change that default active behaviour with pre selected background image before click appears Tamarin. Thank you for your time – RH_Negative Jun 08 '20 at 00:41
  • It is perfectly works, leave above comments. I have one small doubt. How to i add focus to slideshow. Is there any tag available to place in the middle of slide show. Please help me Thank you in Advance – RH_Negative Jun 08 '20 at 03:00
  • What you want is not very clear to me. -1- Do you want something to appear on the bullet that matches the image currently displayed? (e.g image2 is the one currently displayed --> red border on bullet 2) -2- Do you want something different to happen when you click on a bullet? -3- Do you want something different to happen when you hover the bullet? – tamarin Jun 08 '20 at 06:39
  • I edited my answer, you got all the possibilities in it now. – tamarin Jun 08 '20 at 06:55
  • While clicking on navigation bullets slideshow should be fully visible all the time for that can we add focus or some customization that should move cursor focus in the middle of slide show whenever user clicks navigation bullets. To be more specific one user opened the page now the page visible to navigation bullets from top. At this time when user clicks bullets the focus or can scroll to middle of slideshow...? – RH_Negative Jun 08 '20 at 09:12
  • I am not able to attach the photo, you could see the functionality in avaza.com when user clicks icons among any four navigation bullets the slideshow scroll down automatically and visile to user. I meant that functionality Tamarin Thank you – RH_Negative Jun 08 '20 at 10:38
  • I'm sorry, I don't understand you explanations, plus I'm not here to build a functionality just for you. If you want to do something, you have to do some research, try to do it yourself, then if you don't succeed submit a question with your code and then we can help you. If you're happy with the help I provided for this question you can upvote and accept the answer. Thank you! – tamarin Jun 08 '20 at 12:22
  • I am sorry my bad you are not able to understand. You did this for obviously i am upvoting it for sure before that i thought to know about it. while clicking navigation button full slide show should be visible to user if not it should move down. i mean the cursor location – RH_Negative Jun 08 '20 at 12:26
  • No worries man. But I still don't understand what you want. Can you describe it step by step? You arrive on the page what do you see? The slideshow? Only the navigation bullets? What would you like to happen then? What cursor are you talking about? – tamarin Jun 08 '20 at 12:31
  • 1. Once the page loaded, imagine you are seeing upto navigation bullets rest of the contents below you can see while you scrolling down. 2. While scrolling down from navigation bullets you see slideshow first right. 3. To avoid this scrolling while clicking the focus move to slideshow. So two things while clicking 1. Need to slide which is implemented already 2. Need to focus – RH_Negative Jun 08 '20 at 21:56
  • – RH_Negative Jun 08 '20 at 22:00
  • Why did you changed class name for each bullet icons as dots dotOne, dots dotTwo and so on ? – RH_Negative Jun 09 '20 at 16:36
  • So that I could store all of them in a variable (an array in this case) with one line of javascript. `var dots = document.getElementsByClassName("dots");` – tamarin Jun 09 '20 at 22:40
  • So dots holds array values of dotOne,..etc thats is what you are saying right ? – RH_Negative Jun 10 '20 at 00:27
  • dots[0] links to .dotOne element, dots[1] links to .dotTwo element and so on. I felt like this was what you were trying to achieve with your ifs, but it wasn’t working this way... – tamarin Jun 10 '20 at 06:52
  • To make mobile responsive where and all i have to mention size. Do i need use media properties for tablet and mobile separately – RH_Negative Jun 10 '20 at 09:43
  • Document.getElementById or ByClass(dots dotOne) not working in with the class name we preferred so asked Tamarin. How to document.GetElementByClassName with our class name – RH_Negative Jun 10 '20 at 09:46
  • Hey! You need to take more time to write your sentences in english. And reread them before you post. Because it's not understandable. – tamarin Jun 10 '20 at 15:42
  • Awch...!! my bad. How to get value by using class name using Java Script for dots dotOne, dots dotTwo ...etc – RH_Negative Jun 10 '20 at 15:46
  • There is no value to your dots element. Look at the code . – tamarin Jun 11 '20 at 10:20
  • I am trying to implement below function when the user clicks on navigation bullet it should scroll to slide show i have added the function below. Is there any option to get id or class value from dots dotOne, two and so on Tamarin – RH_Negative Jun 11 '20 at 13:24
  • var elmnt = document.getElementById("content"); elmnt.scrollIntoView(); – RH_Negative Jun 11 '20 at 13:24
  • `function goToSlideshow(){ var slideshow = document.getElementsByClassName("slideshow-container"); slideshow[0].scrollIntoView({ behavior: 'smooth' }); }` – tamarin Jun 12 '20 at 00:00
  • `onclick="currentSlide(1); goToSlideshow();"` `onclick="currentSlide(2); goToSlideshow();"` and so on... – tamarin Jun 12 '20 at 00:01
  • I have tried like this and it is not working can you run this on above code snippet tamarin. This is the last change let me upvote once we done this. Thank you so much Tamarin for your time. – RH_Negative Jun 12 '20 at 14:56
  • Updated. The smooth behavior doesn't work with safari though. – tamarin Jun 12 '20 at 15:18
  • I am not using safari, i am using chrome and firebox in both it is not working Tamarin – RH_Negative Jun 12 '20 at 17:46
  • Don't know what to say, it works for me... What is not working? No scrolling? – tamarin Jun 12 '20 at 18:58
  • Just add the function in above code show me please if it is working like you said Tamarin – RH_Negative Jun 14 '20 at 14:11
  • But that's what I did already 2 days ago... Check the snippet again – tamarin Jun 15 '20 at 10:08