In Java script, is there a way to pause a slideshow when hovering over it with the mouse and then have it resume when the mouse is

Hello, I have spent over 10 hours trying to figure out how to pause a slide show when the mouse hovers over it and resume when the mouse moves away. I've looked at some suggestions on stack overflow but couldn't find a solution that works for my code. So, here is my code:

Here is the code snippet:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"/>

<title>Simple jQuery Slideshow from JonRaasch.com</title>

<script type="text/javascript" src="jquery-1.2.6.min.js"></script>

<script type="text/javascript">

/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    // uncomment the 3 lines below to pull the images in random order

    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 1500 );
});

$("#slideshow")(function() { $(this).slides("stop"); });
//playing all slides
$("#slideshow")(function() { $(this).slides("play"); });

</script>

<style type="text/css">

/*** set the width and height to match your images **/

#slideshow {
    position:relative;
    height:350px;
    width: 40%;
}

#slideshow IMG {
    position:absolute;
    top:0;
    left:0;
    z-index:8;
    opacity:0.0;
}

#slideshow IMG.active {
    z-index:10;
    opacity:1.0;
}

#slideshow IMG.last-active {
    z-index:9;
}

</style>

</head>

<body style="font-family: Arial, Sans-serif, sans;">


<!-- this will work with any number of images -->
<!-- set the active class on whichever image you want to show up as the default 
(otherwise this will be the last image) -->

<div id="slideshow">
    <img src="image1.jpg" alt="Slideshow Image 1" class="active" />
    <img src="image2.jpg" alt="Slideshow Image 2" />
    <img src="image3.jpg" alt="Slideshow Image 3" />
    <img src="image4.jpg" alt="Slideshow Image 4" />
</div>






</body>
</html> 

Answer №1

Check out this jsfiddle as a demonstration of what you need.

Your code has some peculiar sections, like

$("#slideshow")(function() { $(this).slides("play"); });
, which I have removed in my example for clarity.

I've incorporated the hover binding from Johan Van den Rym's response into my example.

Essentially, I introduced a boolean variable to track whether the slides are currently being hovered over:

var isPaused = false;
  function switchSlide() {
      if (!isPaused) {
      ...

Additionally, I included hover states at the end:

$("#slideshow").hover(function () {
      isPaused = true;
  }, function () {
      isPaused = false;
  });

Answer №2

$("#slideshow").on("hover", function() {
    $(this).slides("stop");
}, function() {
    $(this).slides("play");
});

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

What is the best way to modify the color of the btn-primary (label) for an unchecked radio button

Objective: I am aiming to change the background color to #FFFFFF when the radio button is not checked and to #9381FF when it is checked. Current Result: Currently, both buttons display a background color of #9381FF. I am aware that using !important is no ...

The webpage Page.php is failing to display the newly added content

I have a collection of logos on my homepage (index.php) in WordPress, and they are positioned at the bottom just as I intended. However, these logos do not appear on all pages. Even if I add the exact same code to the page.php file in the exact position, ...

Navigating to the specific item that a directive is linked to

I am looking to develop a directive that can be applied to elements to adjust their maximum height to be equal to the height of the window minus the distance from the top of the element to the top of the window. I have attempted it in the following manner ...

`Z-index hover problem with jQuery`

Looking for a solution to make a transparent png act as a trigger for starting and stopping a slideshow box underneath it using z-index settings. The current script functions well for the slideshow, but I want the top transparent image (with ID "trans_im ...

Enhance the security of my website by incorporating Google reCAPTCHA into my

When it comes to setting up reCaptcha, most resources suggest using jQuery validation. However, I am unsure about how to implement this when dealing with an ajax form submission. Currently, I am utilizing Validity instead of jQuery Validation. I have the ...

Discover the process of incorporating secondary links into your dabeng organizational chart!

I need to incorporate dotted lines on this chart, such as connecting leaf level nodes with middle level nodes. import OrgChart from '../js/orgchart.min.js'; document.addEventListener('DOMContentLoaded', function () { Mock.mock(&apo ...

Stopping HTTP response in middleware using node.js and express - is it possible?

Currently, I am working on developing a timeout middleware using express in node.js. app.use((req, res, next) => { res.setTimeout(3000, () => { console.warn("We've reached the timeout limit - ending response with status code 408" ...

What sets XHTML 1.1 apart from other document types?

Visit this page for more information: XHTML 1.1 makes it simple to create various display formats, including printing pages, wireless device and PDA pages, and browser pages for television by creating a new CSS (cascading style sheet) for the specific ...

Utilize JQuery in Worklight to link back button functionality with the history feature in HTML5 for seamless navigation

I've come across a few discussions on this topic, but they all assume the presence of a button on the page that allows the user to navigate back. Currently, I'm working on developing a multi-page form using jQuery load and unload in Worklight. Th ...

Transitioning from version 3 to version 4 has resulted in an issue where tooltips are not displaying properly

After following a tutorial on creating a scatter plot using this reference, everything worked perfectly in version 3. However, upon switching to version 4, I encountered issues with the tooltip, x-axis, and y-axis not appearing. To troubleshoot, I created ...

Utilizing Material-UI's DropZone to handle the 'getFileAddedMessage' function, which combines multiple file names into a cohesive string

Currently, I am utilizing the Material UI dropzone feature found at . The issue I am facing is that when dropping multiple files at once, the component combines all file names into a single string on the snack bar alerts. My goal is to have individual snac ...

Full-width Dropdown Menu within a Container with a Maximum Width

Is it possible to make a dropdown menu within a navigation bar open to the full width of the page, even when the parent container has a maximum width of 500px? My attempt to use 100vw resulted in misaligned dropdown elements, and I cannot move the navbar ...

npm: Generating debug and production builds while ensuring accurate dependency management

I am in the process of developing a single page application using TypeScript along with various other dependencies such as jQuery, immutable, lodash, and React. Each resulting module is integrated into the project using requirejs. My goal is to generate t ...

Exploring the power of Express.js by utilizing local variables and rendering dynamic views

I am in the final stages of completing an application using node.js and express, even though I am still relatively new to them. Here's my situation: In my app.js file, I have declared a variable app.locals.webLang and set its initial value to "EN". T ...

How come the behavior of Array.prototype.push() results in creating an endlessly nested array when used on itself?

While testing out the following code: const animals = ['pigs', 'goats', 'sheep']; animals.push(animals) console.log(animals); An error occurred in my testing environment: Issue: Maximum call stack size exceeded. What is c ...

Guide on inserting an item into a vertical navigation menu

When it comes to creating a vertical menu, I rely on Bootstrap 5. https://i.sstatic.net/8CWYK.png The issue I'm facing is how to add a black background to the top of the left menu. https://i.sstatic.net/yoEcO.png I am unsure whether I should imple ...

unable to utilize the library three.js

I stumbled upon an interesting animation on Codepen that uses blocks to reconstruct a map image. The necessary JavaScript files are three.min.js and TweenMax.min.js. I copied the links from Codepen and pasted them into my HTML using <script src="">&l ...

Delineating the Boundaries of an stl Object in three.js

Greetings to all, I have been encountering numerous discussions on this subject, yet none of the proposed solutions seem to work for me. I am currently working with an object that is loaded using the STLLoader in three.js, and I need to obtain its boundin ...

reveal or conceal content on hover of mouse pointer

I am currently working on a section that functions like jQuery tabs, but instead of requiring a click to activate, I want it to work on mouse hover. The current implementation is somewhat buggy - when hovering over a specific section, it displays as expe ...

Errors can be thrown when using React and classNames in the class component

I'm relatively new to React and currently working on a large project. I've run into an issue where I can't use both class and className on all elements and components, particularly custom ones. To work around this, I've had to place the ...