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

How to Align Two HTML Forms Using CSS

I've been experimenting with various methods, but I'm still unable to achieve the desired outcome. Currently, my setup looks like this: However, what I really want is something more like this: The layout consists of a #Container div that conta ...

Expanding Images with JQuery Accordion

I'm facing a problem where I need to include accordions on my website with images, but whenever the accordion is opened, the image ends up stretched. Is there a solution to prevent this from happening? Below is the code snippet I am using: [Fiddle] ...

Unable to activate click function in Jquery

Here is a basic HTML page snippet: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script> <script> $(document).ready(function () { $('#test').click(); }); < ...

What is the best way to show TypeScript code using "<code>" tags within an Angular application?

I am looking to showcase some TypeScript (angular code) as plain text on my website using prismjs. However, the Angular framework is executing the code instead. How can I prevent it from running? I have attempted enclosing it within pre and code tags wit ...

Switch from flexbox layout to begin on the next column within the same row

I am attempting to create a CSS layout where, after a specific element like :nth-child(6), the elements will break and form a separate column within the same row. The parent element should split into 2 columns after every 6th element in the same row, then ...

Struggling to align content vertically within an article and in need of some guidance

Still struggling with vertical alignment of content within an article. I've tried using tables and the vertical-align property but can't seem to make it work. Any suggestions for centering the content responsively on the right-hand side? The lef ...

Guide for sending token through Authorization in Laravel 8 API

I am currently utilizing Laravel 8 as an API REST and encountering an issue where my token is null when sent in the AJAX request. I have successfully handled logins and requests without tokens, but this specific scenario has me puzzled. Within my JavaScri ...

What causes the cancel button to add spaces and create a line break in the textarea field?

Whenever I click the "cancel" button, the textarea inexplicably receives 26 spaces before any existing text and a carriage return after it. This issue occurs when the cancel button is styled in the traditional HTML manner: <button type="reset" name="re ...

HTML - No alterations to Writing Mode

Hello everyone, I'm attempting to display a header vertically in my table but the writing-mode property is not having any effect. CSS .VerticalText { writing-mode: tb-rl; } HTML <td rowspan="6" id="tableHeader" class="Border"> <h1 cl ...

Tips on storing a blob (AJAX response) in a JSON file on your server, not on the user's computer

After researching extensively on creating a URL from or downloading a blob in a computer, I require a unique solution: (1) Save a blob in own server into a JSON file, (2) Optimize the way to update a database using the data stored in the JSON. My attemp ...

Is there a way to update page content without having to refresh the entire page?

My goal is to refresh specific content on a page without having to reload the entire page using JavaScript or jQuery. Since my project is built in PHP and JavaScript, I encountered this issue. Note : I want the page content to refresh when a user performs ...

Preventing a sprite from going beyond the boundaries of a canvas with pure JavaScript - here's how!

I am in the process of developing a game using native JavaScript, HTML, and a touch of CSS. I have two blocks called Sprites that tend to keep moving off the canvas edges when they reach them. Question: How can I utilize native JS to prevent the sprites fr ...

I'm looking for a way to have an element shift in a particular direction only when two keys are pressed simultaneously

Having trouble moving a square diagonally when two keys are pressed simultaneously. Trying to create an if statement that detects both key presses at the same time and triggers the movement. However, all attempts have failed so far, leaving uncertainty abo ...

LabeFor does not automatically create the display-lable class attribute

When setting up a new MVC project, the default Site.css file typically includes the following styles: /* Styles for editor and display helpers ----------------------------------------------------------*/ .display-label, .editor-label { font-weight: ...

Is it possible to modify the dropdown menu so that it opens on the right side instead of using a select tag?

Is there a way to make the drop-down from the select tag open to the right side(drop-right)? <label for="ExpLabel">Select Message Expiry:</label> <select name="ExpSelect" id="Expiry"> <option value="ExpiryDate">1 Day</opt ...

Access previous value in Vuejs onchange event

In the code snippet below, how can I retrieve the previous value of the model that has been changed, specifically the age in vuejs? var app = new Vue({ el:"#table1", data:{ items:[{name:'long name One',age:21},{name:'long name Two&a ...

Choose the option to retrieve information from the database

Within my database, there is a table named "train_information" which contains the following fields: train_id country_id user_id train_name tare_weight number_of_bogies number_of_axles wheel_diameter_min wheel_diameter_max In addition, I have developed t ...

Using the <ins> element to push content into a separate div lower on the webpage

My text inside the <p> element is being pushed down by the presence of the <ins> tag. Removing the <ins> tag from the DOM using developer tools results in my text returning to its expected position. main { max-width: 1000px; ma ...

Hover effect on icons within the framework with jquery-ui

I am looking to implement a feature with jquery-ui icons that will change style when the user hovers over them. However, I've found that it only works if they are contained within another element, which then creates an unwanted box around the icons. ...

Add data to a DataTable without the need to manually refresh the page

I am looking to dynamically append a DataTable on my webpage. The goal is to have a form that users can fill out multiple times and each time they submit their inputs, the results will be added to the DataTable for display. <table id="table" class="di ...