在jQuery / javascript中图像上的悬停效果

啦啦

在提供帮助之前,我知道CSS会容易得多,但是请不要提供CSS解决方案,因为我被告知只能触摸.js文件。

我不确定我在网上搜索时代码中的错误所在,这就是我认为应该在代码中所包含的内容,但是我的图像完全没有我想要的不透明性和持续时间效果。

这是javascript所涉及的HTML的一部分:

<div id="content">
    <div id="myCols" class="container">
        <div class="col1">
            <img src="images/boxImage1.jpg" class="imageCS"/>
        </div>
        <div class="col2">
            <img src="images/boxImage2.png" class="imageCS"/>
        </div>
        <div class="col3">
            <img src="images/boxImage3.jpg" class="imageCS"/>
        </div>
    </div>
</div>

这是我键入的javascript:

$(document).ready(function()
{


    $("imageCS").hover(function()

        //hover over opacity 30% at 0.5 sec
        $(this).stop().animate({opacity: "0.3"}, "0.5"); 
    ),

    function()
    (
        //hover out opacity 100% at 1 sec
        $(this).stop().animate({opacity: "1"}, "1"); 
    )




});

我不确定什么是错的,因为效果甚至没有发生。

阿夫南·艾哈迈德(Afnan Ahmad)|

.您的元素选择中缺少,您可以使用mouseovermouseout

$('.imageCS').mouseover(function(){
    $(this).css('opacity','.2');
    
}).mouseout(function(){
    $(this).css('opacity','1');
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="http://placehold.it/350x150" class="imageCS"/>

如果要使用,hover请参见以下代码片段:

 $('.imageCS').hover(		
   function () {
     $(this).css('opacity','.2');
   }, 
   function () {
     $(this).css('opacity','1');
   }
 );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="http://placehold.it/350x150" class="imageCS"/>

调用$(选择器).hover(handlerIn,handlerOut)的简写为:$(选择器).mouseenter(handlerIn).mouseleave(handlerOut);

在此处检查文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章