Is there a way to stop the modal from constantly fading in and out of the slider when clicked?

There is a slider with 3 modals set to appear at intervals, covering the slides with gradients. When a user clicks on a slide, the autoplay stops and allows them to select any image they want.

The issue is that the modals continue to appear and disappear based on the timer interval. How can I make each modal display without fading in and out once the user interacts with the previous or next arrow?

In this scenario, the "HI", "HOW", "ARE YOU?" messages would stay fully displayed in their respective sliders and not fade in and out anymore after the user clicks on either of the arrows.

$(document).ready(function() {
  $(".myModal").delay(3000).fadeIn().hide();
  $(".myModal2").delay(6000).fadeIn().hide();
  $(".myModal3").delay(9000).fadeIn().hide();
});
      
$(document).ready(setInterval(function() {
  $(".myModal").delay(3000).fadeIn().hide();
  $(".myModal2").delay(6000).fadeIn().hide();
  $(".myModal3").delay(9000).fadeIn().hide();
},12000)); 
 
var currentIndex = 0,
items = $('.container div'),
itemAmt = items.length;

//cycleItems function
function cycleItems(){
  var item = $('.container div').eq(currentIndex);
  items.hide();
  item.css('display', 'inline-block');
}; //end cycleItems function

//begin autoSlide function
var autoSlide = setInterval(function() {
  currentIndex ++;
  if( currentIndex > itemAmt -1){
    currentIndex = 0;
  }
  cycleItems();
},3000)
//end autoSlide function

//Next slider code
$('.next').click(function () {
  clearInterval(autoSlide);
  currentIndex += 1;
  if (currentIndex > itemAmt - 1){
    currentIndex = 0;
  }
  cycleItems();
});

//Previous slider code
$('.prev').click(function(){
  clearInterval(autoSlide);
  currentIndex -= 1;
  if(currentIndex < 0){
    currentIndex = itemAmt - 1;
   }
   cycleItems();
});
.container {
  width: 100%;
  background-color: black;
  margin: 0 auto;
  text-align: center;
  position: relative;
  overflow: hidden;
}
.container div {
  width: 100%;
  display: inline-block;
  display: none;

}
.container img {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  text-align: center;
}

button {
  position: absolute;
}

.next {
  right: 5px;
  cursor: pointer;
  position: absolute;
  background-image: url('https://image.freepik.com/free-icon/arrow-bold-right-ios-7-symbol_318-35504.jpg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  width: 100px;
  height: 100px;
  z-index: 500;
  top: 50%;
}

.prev {
  left: 5px;
  cursor: pointer;
  position: absolute;
  background-image: url('https://image.flaticon.com/icons/svg/17/17264.svg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  width: 100px;
  height: 100px;
  z-index: 500;
  top: 50%;
}

.image2 {
  background-image: url('https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png');
  background-position:center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}

.image3 {
  background-image: url('https://static.comicvine.com/uploads/square_small/11/114183/5147875-lisa_simpson.png');
  background-position:center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}

.image4 {
  background-image: url('https://vignette2.wikia.nocookie.net/simpsons/images/a/ab/BartSimpson.jpg/revision/latest?cb=20141101133153');
  background-position:center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}

.image1 {
  background-image: url('http://media.oregonlive.com/ent_impact_music/photo/9905096-large.jpg');
  background-size: contain;
  background-position:center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
  z-index: 200;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container image1">
  <div class="image1"></div>
  <div class="image2">
   <section class="myModal">HI</section>
  </div>
  <div class= "image3">
     <section class="myModal2">HOW</section>
  </div>
  <div class="image4">
     <section class="myModal3">ARE YOU?</section>
  </div>
</div>

<div class="next"></div>
<div class="prev"></div>

Answer №1

You appear to have inadvertently duplicated the auto slide feature in your code. To resolve this issue, here is a revised example that combines the auto and manual sliders to function properly.

$(document).ready(function() {
  var currentIndex = 0;
  var items = $('.container div');
  var itemAmt = items.length;

  //cycleItems function
  function cycleItems(direction, fade) {
    currentIndex += direction;
    if (currentIndex > itemAmt - 1) {
      currentIndex = 0;
    } else if (currentIndex < 0) {
      currentIndex = itemAmt - 1;
    }
    var item = items.eq(currentIndex);
    items.hide();
    if (fade) {
      item.fadeIn();
    } else {
      item.show();
    }
    
  };
  //end cycleItems function

  // start autoSlide
  cycleItems(1, true);
  var autoSlideInterval = setInterval(cycleItems, 3000, 1, true);

  //Next slider code
  $('.next').click(function() {
    clearInterval(autoSlideInterval);
    cycleItems(1, false);
  });

  //Previous slider code
  $('.prev').click(function() {
    clearInterval(autoSlideInterval);
    cycleItems(-1, false);
  });
});
.container {
  width: 100%;
  background-color: black;
  margin: 0 auto;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.container div {
  width: 100%;
  display: inline-block;
  display: none;
}

.container img {
  width: 100%;
  height: 100%;
  margin: 0 auto;
  text-align: center;
}

button {
  position: absolute;
}

.next {
  right: 5px;
  cursor: pointer;
  position: absolute;
  background-image: url('https://image.freepik.com/free-icon/arrow-bold-right-ios-7-symbol_318-35504.jpg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  width: 100px;
  height: 100px;
  z-index: 500;
  top: 50%;
}

.prev {
  left: 5px;
  cursor: pointer;
  position: absolute;
  background-image: url('https://image.flaticon.com/icons/svg/17/17264.svg');
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  width: 100px;
  height: 100px;
  z-index: 500;
  top: 50%;
}

.image2 {
  background-image: url('https://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png');
  background-position: center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}

.image3 {
  background-image: url('https://static.comicvine.com/uploads/square_small/11/114183/5147875-lisa_simpson.png');
  background-position: center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}

.image4 {
  background-image: url('https://vignette2.wikia.nocookie.net/simpsons/images/a/ab/BartSimpson.jpg/revision/latest?cb=20141101133153');
  background-position: center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}

.image1 {
  background-image: url('http://media.oregonlive.com/ent_impact_music/photo/9905096-large.jpg');
  background-size: contain;
  background-position: center;
  display: block;
  background-repeat: no-repeat;
  width: 100%;
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container image">
  <div class="image1">
    <br>
  </div>
  <div class="image2">
    <section class="myModal2">HI</section>
  </div>
  <div class="image3">
    <section class="myModal3">HOW</section>
  </div>
  <div class="image4">
    <section class="myModal4">ARE YOU?</section>
  </div>
</div>

<div class="next"></div>
<div class="prev"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript game security with Ajax

My JavaScript game, created using jQuery, involves storing the player's position in a database. When the character is in motion, a request is sent to a specific URL such as mysite.com/map/x1/y3 (where x=1, y=3 represents the character's position) ...

Guide on toggling visibility of a column in Material-ui based on a conditional statement

Can someone help me with code that allows me to hide or show the TableCell based on an if statement? I am currently working with MATERIAL-UI framework which can be found here: https://material-ui.com/ <TableBody> {Object.entries(servicesCode).map ...

Is there a way for me to hear events from different applications?

Exploring the development of an electron app. Interested in incorporating an event listener into external applications to generate a console message based on certain actions. For instance, monitoring the opening of a new tab in Chrome and displaying a me ...

Questions about clarifying JS promises and async-await functions

After doing some reading on promises in JavaScript, I have come across conflicting information which has left me with a few basic questions. I have two specific questions that need clarification: Is it necessary for every function in JavaScript to be ca ...

Tips for iterating through JSON data and combining rows with matching state IDs

I have a JSON file containing data about US senators, with two senators listed for each state. My goal is to iterate over this JSON file and create a new JavaScript object where the state ID serves as the key, with two properties assigned to each key: name ...

Issue with updating patch version using NPM standard-version

I have integrated standard-version into my javascript project. I made sure to include the release script in my package.json: "scripts": { ... "release": "standard-version" } However, after adding a commit with the mes ...

Node.js Promise Rejection: TypeError - Unable to access property 'sign' because it is undefined

tran_script.js const CoinStack = require('coinstack-sdk-js'); const coinstackClient = new CoinStack('YOUR_COINSTACK_ACCESS_KEY', 'YOUR_COINSTACK_SECRET_KEY'); // Actual keys not displayed const privateKeyWIF = CoinStack.ECK ...

Should I include jquery.js or bootstrap.js from my own domain or stick to the original source?

I'm facing a dilemma on whether I should host jquery.js or bootstrap.js files on my own domain, or simply link directly to the download page of the providers. What is the most common practice in this scenario? My main goal is to optimize the loading s ...

Prevent direct prop mutation in Vuetify's Dialog component to prevent errors

I am facing an issue with my child component, a dialog box where I pass the prop dialog from the parent component. Whenever I try to close it by changing the prop value, I receive a warning. Can someone please guide me on the correct approach to resolve ...

Retrieving and storing data between a database and text fields

Trying: Extracting information from the database to text fields (the database contains infinite data so it is not feasible to name each field individually), then modifying the data and updating it back to the database. Issue: Unable to assign names to th ...

A specialized Discord bot designed to capture and forward all direct messages to a designated webhook

I am looking to develop a bot using discord js v13 that can intercept all DM messages and send them to a webhook. Additionally, I want the bot to send a reply back to the sender once the admin gives the command '/reply [text]'. I am unsure of how ...

What is the equivalent in AngularJS to triggering a form submission event like jQuery's $("#formID").submit()?

After the completion of a form with various fields, I require the user to either register or login in a modal window before submitting the form. The process involves opening a modal window with a login form when a user attempts to submit the main form. On ...

Setting the height of a div to 100% is not effective

I've designed a two-column layout. You can view the code on jsfiddle.net. My problem is that I want the center line with a width of 10px to have a height of 100%. I have a container div with the ID #obal (set to height: auto). When I set the height of ...

Updating the TextField in React Material UI based on the Slider value without affecting the Slider

Is there a way to update the value of a TextField based on the slider, while still allowing manual input in the TextField? I want the TextField to reflect changes from the slider but not vice versa. I attempted making the TextField a controlled component ...

The footer hovers above the div element

Currently, I am facing an issue where my footer is constantly floating on top of my div. Surprisingly, this problem only occurs on mobile and tablet resolutions while working perfectly fine on desktop view. The main problem lies in the fact that the foo ...

Tips for centering a video vertically within a cell

I need assistance with aligning a video in the center of a fixed height cell while hiding the overflow at the top and bottom. Here is what I have so far: <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height ...

Modify the color or background color of a disabled Material UI checkbox

The disabled unchecked checkbox appears too subtle to me, and I would like to enhance it by giving it a grey background and changing the cursor style to not-allowed. I've been trying to implement these styles on the checkbox using the makeStyles, but ...

Error in Node.js: Attempting to modify headers after they have already been sent to the client

I've been facing the challenge mentioned in the topic for quite some time now. Is there anyone who can assist me with this? Feel free to ask any questions if you need clarification. I've gone through a few potential solutions for this issue, but ...

Functions perfectly on Chrome, however, encountering issues on Internet Explorer

Why does the Navigation Bar work in Chrome but not in Internet Explorer? Any insights on this issue? The code functions properly in both Internet Explorer and Chrome when tested locally, but fails to load when inserted into my online website editor. Here ...

Retrieving a completely random caption from a text file with JavaScript and showcasing it in HTML

I need help loading a random caption each time my page loads. I have a text file with a string on each line, but I'm new to HTML and JavaScript. Here's my code: <div class="centerpiece"> <h1>DEL NORTE BANQUEST</h1 ...