Downsides of using MouseOver on websites

Currently, I am working on creating a unique "button" that contains multiple images to create a slideshow effect. Whenever the button is hovered over, the image changes to another one.

However, there's a problem - when the slideshow image changes, the hover effect is lost as the mouse technically moves away from the image.

I attempted to incorporate a fading effect into my button, but most resources suggest using hover functions instead of MouseOver and MouseOut events.

This begs the question: which is better in terms of potential capabilities - Hover or MouseOver?

Is it feasible to pause the slideshow when hovering over it? And if so, how can I achieve this?

Here is the current code snippet:

function.js

$(function () {

    $('#01 img:gt(0)').hide();
    setInterval(function () {
        $('#01 :first-child').fadeOut(1500)
           .next('img').fadeIn(1500)
           .end().appendTo('#01');
    },
      3000);
});

$(document).ready(function () {

    $("#image1").mouseover(function () {
        $(this).attr("src", "images/board_01_over.jpg");
    });

    $("#image1").mouseout(function () {
        $(this).attr("src", "images/board_01_01.jpg");
    });
});

main.css

#board {
    float: left;
    width: 998px;
    overflow: hidden;
}

.fadein {
    float: left;
    position: relative;
    width: 240px;
    height: 140px;
    margin: 1px 1px 1px 1px;
}

.fadein img {
    position: absolute;
    left: 0;
    top: 0;
    height: 140px;
    opacity: 0.6;
    overflow: hidden;
}

.fadein img:hover {
    opacity: 1;
}

Main.html

<div id="board">
    <div class="fadein" id="01">
        <img src="images/board_01_01" id="image1" />

        <img src="images/board_01_02.jpg" id="image2" />
    </div>
</div>

Answer №1

Using jQuery allows you to employ the hover() function.

http://api.jquery.com/hover

$("#image1").hover(function () {
    $(this).attr("src", "images/board_01_over.jpg");
},

function () {
    $(this).attr("src", "images/board_01_01.jpg");
});

To manage your slider effectively, consider creating an object for better control.

var Slideshow = {
    interval:null,

    start: function () {
        ...
        initialize
        ...
        // keep track of the interval ID for stopping later
        this.interval = window.setInterval(this.next, 3000);
    },

    next: function () {
        /*
         * The keyword this cannot be referenced in this function
         * as it is executed outside the object's context.
         */
        ...
        implement your logic
        ...
    },

    stop: function () {
        window.clearInterval(this.interval);
    }
};

You can now easily use

Slideshow.start();
Slideshow.stop();

to initiate and halt your slider from any part of your code.

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

Would you like to justify to the right?

I am attempting to align the final element in a list to the right side: <ul> <li>one</li> <li>two</li> <li>three</li> <li id="right">right?</li> </ul> li { display: inline-bl ...

JQuery submit event not triggering object setting

I'm looking to gather input values from a form and store them in an object for an offer. After trying the following code on submit: $(document).ready(function(){ $('#formOffre').on('submit', function(e) { e.preventDefault ...

Clearing the Terminal Console Using JavaScript

Currently, I am in the process of developing an RPG CLI Game using JavaScript and Node.js. The game consists of multiple pages such as a menu for selecting options and the actual fight sequence. One issue I am facing is that whenever there is a change in t ...

Having Trouble Rendering EJS Files in HTML: What Am I Doing Wrong?

I'm having trouble displaying my EJS files as HTML. Whenever I try to access my EJS file, I receive a "Cannot GET /store.html" error message. if (process.env.NODE_ENV !== 'production') { require('dotenv').config() } const stri ...

Enhancing the Performance of jQuery Scripts

My jQuery function called textfill is utilized on numerous div elements. Essentially, it resizes the text inside each div to perfectly fit the container so that longer texts are made smaller than shorter ones while maintaining the maximum font size without ...

Acquire HTML using Java - certain characters are not retrieved accurately

Currently, I am facing an issue with my Java code when trying to fetch HTML from Turkish webpages. It seems that my Java code is struggling to recognize certain Turkish characters. Below is the Java code snippet I am using: import java.io.BufferedInputStr ...

Illuminate a corresponding regular expression within a text input

I currently have a functional code for testing regex, which highlights matching patterns. However, my challenge lies in highlighting the result within the same input as the test string. Below you will see the HTML and JavaScript snippets along with two ima ...

What is the best way to select an ID element using JQuery?

Everything is going smoothly, could you please assist me with a question. Here is the HTML form code I am working with: <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <scri ...

Personalize scrollbar appearance in Mozilla Firefox and Internet Explorer

When it comes to customizing the scrollbar in chrome, CSS makes it easy: ::-webkit-scrollbar { width: 7px; } Unfortunately, this method does not have the same result in Firefox (version 38) and IE (version 11). I attempted the following code as an alter ...

A step-by-step guide for resolving the 'variable is not constructor' issue in a discord.js node project

Currently, I am experimenting with a discord bot and attempted to establish a record in the database. However, I encountered some issues along the way. The MongoDB server is operational and working as expected. There are two essential files for this proj ...

Using Jquery to select elements with specific text using the :

I am trying to execute 'action one' when the anchor variable includes the word 'image'. If it does not include that specific text, then 'action two' should be executed instead. var clickedImage = $('.header ul li') ...

Library for implementing stylish font effects in JavaScript

I remember stumbling upon a unique JavaScript library that enabled users to write text in different styles and formats. Unfortunately, I am having trouble finding it again. Can anyone suggest any helpful links? Thank you, Murtaza ...

Hovering over a Div tag to slide it out

I need help creating an animation that makes a div slide out when the mouse hovers over it, and stay out until the mouse moves away. I'm new to React so any guidance would be appreciated. Here is my code snippet: return ( <div id="homeDiv"> ...

Swap out the image source when the mouse hovers over it and revert back to the original when

Here is my HTML code for one larger image with two smaller thumbnails beneath it: <img src="../images/image.jpg" class="left main" alt="main image" /> <img src="../images/image2-thumb.jpg" class="left mainthumb" alt="image two" /> <img sr ...

What is the best way to retrieve a LinkedIn profile URL by utilizing the LinkedIn API?

I have successfully retrieved the first and last name, email, positions, etc., but I am having trouble finding the command to fetch the URL for the linkedin profile. My goal is to send linkedin profile URLs through my web app. Below is the code snippet I ...

Unsure how to proceed with resolving lint errors that are causing changes in the code

Updated. I made changes to the code but I am still encountering the following error: Error Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. It is recom ...

Tips for formatting numbers within a chart

One issue in my chart is that the values are not formatted correctly. For instance, I have a number 15900 and I would like it to be displayed as 15 900 with a space between thousands and hundreds. Below is the code snippet I would like to implement for s ...

Clicking inside the bootstrap modal does not trigger the ng-click event

I encountered an issue while trying to implement the "ng-click" function from Angular into an HTML page. It worked initially, but when I tried using the same "ng-click" function within a Bootstrap modal, the code stopped functioning. Can anyone explain why ...

Exploring the world of nested routes in Angular and how to efficiently

Hey there! I'm brand new to all of this and still trying to wrap my head around a few things, so any guidance you can offer would be awesome! :) Overview I've got a bunch of projects (/projects) Clicking on a project takes me to a detailed sum ...

Create a responsive iframe that expands to fullscreen when a button is clicked

Currently, I am using the following code to create an adaptive iframe with a fixed ratio for both width and height: <div id="game-container" style="overflow: hidden; position: relative; padding-top: 60%"> <iframe style="position: absolute; width ...