简体   繁体   中英

jQuery mouseenter tips?

I'm using jQuery to manipulate opacity, and it works pretty good. However, the perfectionist that I am, it annoys me that the .on('mouseover') function fires only after the past .on('mouseover)-function is finished.

HTML:

<div class="feature avans">
    <div class="feature-media">
        <img src="assets/img/avans.png"/>
    </div>
    <a href="">
        <div class="text-wrap">
            <p class="big">Avans</p>
            <h1>Finding work force &mdash; simplified.</h1>
        </div>
    </a>
</div>

CSS:

.feature-wrap {
  width: 1144px;
  margin: 0 auto;
  padding: 0;
}
.feature-wrap .feature {
  width: 100%;
  padding: 0;
  overflow: hidden;
  position: relative;
  background: black;
}
.feature-wrap .feature .feature-media {
  width: 100%;
  opacity: 0.85;
}
.feature-wrap .feature .feature-media img {
  width: 100%;
  height: 350px;
  margin: 0 0 -5px;
  padding: 0;
}
.feature-wrap .feature .text-wrap {
  width: 50%;
  position: absolute;
  top: 0;
  padding: 30px 60px 0;
}

jQuery:

$('.feature').on('mouseenter', function () {
    $(this).children('.feature-media').animate({opacity: 0.93}, 150);
});

$('.feature').on('mouseleave', function () {
    $('.feature-media').animate({opacity: 0.85}, 150);
});

Also, I would love it if someone knew if there's a way to make the function work more like the CSS native hover pseudo-selector (ie the effect only fires while the mouse is in fact hovering the element in question).

Use .stop() :

$('.feature').on('mouseenter', function () {
    $(this).children('.feature-media').stop().animate({opacity: 0.93}, 150);
});

$('.feature').on('mouseleave', function () {
    $('.feature-media').stop().animate({opacity: 0.85}, 150);
});

This will stop any previous animations or functions, and start the new one.

I love jQuery but my best tip here is to use css3 transitions... They are much more efficient and prevent asynchronous JavaScript code.

.feature{
   -webkit-transition:0.15s linear;
   -moz-transition:0.15s linear;
   -ms-transition:0.15s linear;
   -o-transition:0.15s linear;
   transition:0.15s linear;
   filter: alpha(opacity=93);
   opacity:0.93;
}
.feature:hover{
    filter: alpha(opacity=85);
    opacity:0.85;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM