Home  >  Article  >  Web Front-end  >  Method to create mouseover effect based on jQuery_jquery

Method to create mouseover effect based on jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 16:11:021387browse

The example in this article describes how to create a mouse hover effect based on jQuery. Share it with everyone for your reference. The specific implementation method is as follows:

1. Create HTML:

<ul>
<li><a href="/tv"><img src="images/tv_off.gif" class="mainnav"></a></li>
</ul>

2. Select the class of .mainnav:

$(".mainnav").hover(
 function () {
 },
 function () {
 }
);

3. Create variable imgPath and specify image SRC:

$(".mainnav").hover(
 function () {
  // Grab the source
  var imgPath = $(this).attr("src");
 },
 function () {
  // Grab the source
  var imgPath = $(this).attr("src");
  }
);

4. Find the string off and replace it with on:

$(".mainnav").hover(
 function () {
  // Grab the source
  var imgPath = $(this).attr("src");
  // Replace the path in the source
  $(this).attr("src",imgPath.replace("off", "on"));
 },
 function () {
  // Grab the source
  var imgPath = $(this).attr("src");
  // Replace the path in the source
  $(this).attr("src",imgPath.replace("on", "off"));
  }
);

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn