Hover over an element repeatedly to trigger an action with jQuery, as opposed to just once

Is it possible to have my p tag transition whenever I hover over it? I am looking to create a hover effect on an image that displays text in a div tag with scaling transitions. Can someone assist me with this? Instead of using jQuery or JavaScript to dynamically change the text, I want the transition to occur each time I hover over the image. Below is a snippet of my code:

var tex = ["photoshop", "javascript", "food blog"];

$(document).ready(function() {
  var text=$('.box p').text();

  $('.one').hover(function() {
    $('.box p').text(tex[0].toUpperCase());
    $('.box p').transition({ scale: [1.2] });
  },function(){
    $('.box p').text(text);
  });

  $('.two').hover(function() {
    $('.box p').text(tex[1].toUpperCase()); 
    $('.box p').transition({ scale: [1.2] });
  },function(){
    $('.box p').text(text);        
  });

  $('.three').hover(function() {
    $('.box p').text(tex[2].toUpperCase()); 
    $('.box p').transition({ scale: [1.2] });
  },function(){
    $('.box p').text(text);        
  });
});
.image:hover {
  -webkit-filter: brightness(.5);
  filter: brightness(.5);
}

.image img {
  height: 200px;
}

* {
  margin:0;
  padding:0;
}

.box{
  width: 180px;
  height: 40px;
  margin:  100px auto;
  background-color: black;
}

.box p {
  line-height: 40px;
  text-align: center; 
  font-size: 1.5em;
  color: white;
  font-family: sans-serif;
  font-weight: bold;
  transform: scale(0.75);
  transition: transform 2s linear;
}

.container {
  text-align: center;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="http://ricostacruz.com/jquery.transit/jquery.transit.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">
  <p></p>
</div>
<div class="container">
  <div class="row">
    <div class="col-lg-4">
      <div class="one image">
        <img src="https://images.unsplash.com/photo-1457464901128-7608dc7561ea?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8f43ead700f533404bd37bcbb5cfd679&auto=format&fit=crop&w=750&q=80">
      </div>
    </div>
    <div class="col-lg-4">
      <div class="two image">
        <img src="https://images.unsplash.com/photo-1488590528505-98d2b5aba04b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=e3b92161cef01773ab8e0f83d4da1126&auto=format&fit=crop&w=750&q=80">
      </div>
    </div>
    <div class="col-lg-4">
      <div class="three image">
        <img src="https://images.unsplash.com/photo-1481487196290-c152efe083f5?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=527bc5d7e466efd5ce8a98c311728fbd&auto=format&fit=crop&w=830&q=80">
      </div>
    </div>
  </div>
</div>

Answer №1

hover()

When using the hover() function in jQuery, you are binding one or two handlers to the selected elements. These handlers will be triggered when the mouse pointer enters and leaves the elements.

If you have set up a transition for the mouseenter event but not for mouseleave, you can achieve the same effect by using separate mouseenter() and mouseleave() functions as shown below.

$(...).hover(function(){
  // ...
},function(){
  // ...
})

This is equivalent to:

$(...).mouseenter(function(){
  // ...
}).mouseleave(function(){
  // ...
})

In addition, if you want to bind events to elements with classes .one, .two, and

.three</code within an element with class <code>.image
, you can follow the code snippet provided below.

The following script changes the text inside a box when hovering over an image, along with applying some styling effects.

var tex = ["photoshop", "javascript", "food blog"];

$(document).ready(function() {
  var text=$('.box p').text();
  
  $('.image').hover(function(){
    $('.box p').text(tex[0].toUpperCase());
    $('.box p').transition({ scale: [1.2] });
  },function(){
    $('.box p').text(text);
    $('.box p').transition({ scale: [1] });
  });
});
.image:hover {
  -webkit-filter: brightness(.5);
  filter: brightness(.5);
}

.image img {
  height: 200px;
}

* {
  margin:0;
  padding:0;
}

.box{
  width: 180px;
  height: 40px;
  margin:  100px auto;
  background-color: black;
}

.box p {
  line-height: 40px;
  text-align: center; 
  font-size: 1.5em;
  color: white;
  font-family: sans-serif;
  font-weight: bold;
  transform: scale(0.75);
  transition: transform 2s linear;
}

.container {
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.transit/0.9.12/jquery.transit.min.js"></script>
<div class="box">
  <p></p>
</div>
<div class="container">
  <div class="row">
    <div class="col-lg-4">
      <div class="one image">
        <img src="https://images.unsplash.com/photo-1457464901128-7608dc7561ea?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=8f43ead700f533404bd37bcbb5cfd679&auto=format&fit=crop&w=750&q=80">
      </div>
    </div>
    <div class="col-lg-4">
      <div class="two image">
        <img src="https://images.unsplash.com/photo-1488590528505-98d2b5aba04b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=e3b92161cef01773ab8e0f83d4da1126&auto=format&fit=crop&w=750&q=80">
      </div>
    </div>
    <div class="col-lg-4">
      <div class="three image">
        <img src="https://images.unsplash.com/photo-1481487196290-c152efe083f5?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=527bc5d7e466efd5ce8a98c311728fbd&auto=format&fit=crop&w=830&q=80">
      </div>
    </div>
  </div>
</div>

Answer №2

Insert the following code snippet:

        $('.one, .two, .three').mouseleave(function () {
            $('.box p').css("transform","");
        })

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

Creating dynamic selection options in an HTML select tag using PHP

When retrieving category and sub-category information from an API json file, the API returns category objects with a "parent" attribute. Main category objects have a parent attribute equal to 0, and sub-category objects have the parent attribute equal to t ...

Having trouble displaying Ad-Gallery Loader.gif?

Having trouble with an image gallery I found at . The loader.gif image isn't showing up in IE9, just a red X. Being new to JavaScript, I'm struggling to fix this issue located at the top of the JavaScript file. (function($) { $.fn.adGallery = ...

Troubleshooting problems with the CSS code for a progress bar in Gmail

I recently came across a unique progress bar design on Gmail and wanted to implement something similar. I found some code snippets on this webpage: . However, I encountered an issue where the progress bar was only displaying in Internet Explorer but not in ...

Changing Bootstrap variables within a React application: A step-by-step guide

I've been working with facebook's create-react-app and I successfully added SASS to my project. Now, I'm trying to incorporate Bootstrap and customize the theme but running into some issues. Here's what my setup looks like: package.jso ...

Transforming data from PHP JSON format into HTML output

Struggling to convert JSON data into HTML format? Having trouble maintaining the correct order of child elements in your structure? Here's a solution: Take a look at this example JSON: { "tag": "html", "children": [ { "ta ...

What is the most effective way to identify mobile browsers using javascript/jquery?

As I develop a website, I am incorporating image preloading using JavaScript. However, I want to ensure that the preload_images() function is not called for users on slow bandwidth connections. In my experience, the main demographic with slow internet spe ...

Is there a way to ensure that when displaying information boxes with Bootstrap, the inputs do not get misplaced or shuffled to the wrong location

I have been working on a form that needs to display an alert: When clicking the button next to the input control, an alert should appear. Although the alert is showing correctly, the input controls are shifting to the wrong position afterwards: The desir ...

Excessive form inputs extend beyond the modal when utilizing Bootstrap 3

Having an issue loading a modal with Angular and populating it with a template. The main problem I'm facing is that the inputs are extending beyond the boundaries of the modal - attached below is a screenshot illustrating the problem: Below is the c ...

Instructions on how to change database entries utilizing text input fields, enabling users to apply modifications by selecting the 'update' button

I'm a beginner when it comes to PHP and MySQL. Right now, I have a webpage with multiple text boxes that display data from my database. I am looking for guidance on how to update this data by allowing users to make changes in the textboxes and then cl ...

Storing the closed state of a pop-up box in localStorage for future reference

I'm struggling to get localStorage working properly on my website (). Here's what I need to achieve: A newsletter subscription pop-up on every page - this part is functioning correctly An option for users to click 'X' to close the po ...

Issue with Datepicker functionality within the <th> element is causing it to malfunction

Currently, I am attempting to utilize the Bootstrap datepicker, but I am facing an issue when trying to implement it within a table, specifically inside a th element. Below is the structure of my table: <table id="table" class="table table-bord ...

Keep the HTML video link out of sight in the Control Center on iOS devices

Looking for a way to conceal the video URL from appearing in the iOS Control Center, can anyone provide a JavaScript or HTML code solution? https://i.sstatic.net/NI79O.png ...

Determine if the server is operational using Microsoft Edge

To verify the status of my server, I have implemented the code below: <head> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script> function checkServerStatus() { var img = document. ...

Input form with multiple fields. Automatically display or hide labels based on whether each field is populated or empty

I am attempting to create a form where the placeholders move to the top of the input when it is focused or filled. However, I have encountered an issue with having multiple inputs on the same page. Currently, my JavaScript code affects all inputs on the pa ...

Chrome miscalculates the dimensions of the screen

This is my first attempt at working with responsive design, and I seem to have encountered a bug. When I shift part of the browser off-screen and expand it to around 1345 pixels, it seems to trigger the CSS set for 1224 pixels. //Currently, only @media al ...

Generate a unique token through a personalized form

**HTML** <div ref="addCardForm" id="card-element"> <div class="card_digit_text">Credit Card Number <input type="text" id="cardDigit" name="email" placeholder="0000 0000 0000 0000"> </div> ...

How to ensure a table in MaterialUI always maintains full width without requiring horizontal scrolling in CSS?

After attempting to allocate each widthPercent value on the TableHeader so they sum up to 100%, I am still experiencing the presence of a scroll bar. I have tried setting fixed widths for each column, but this results in excess space on the right side dep ...

Steps to include a HTML webpage within another page

I need assistance with a scenario involving two separate HTML pages on different servers. Merchant Form - Server A Payment Form - Server B Here's the desired scenario: The user completes all necessary fields on the Merchant Form and clicks submit. ...

What is the purpose of a form that includes a text input field and a button that triggers a JavaScript function when the enter key is pressed?

<form action=""> <input id="user_input" onKeyDown="if(event.keyCode == 13)document.getElementById('okButton').click()" > <input id="okButton" type="button" onclick="JS_function();" value="OK"> </form> I'm trying to a ...

Can you attach a jQuery function to multiple CSS selectors at once?

Is it feasible to bind a mouseout call to two CSS selectors in order to trigger another action when the mouse moves away from both elements simultaneously? ...