简体   繁体   English

当我将鼠标悬停在图像上时,我需要使用 JavaScript 更改图像的不透明度值,但我的代码不起作用

[英]I need to change the opacity value of an image using JavaScript when i mouse over it but my code isn't working

I need the images opacity to increase when I mouse over and decrease when I mouse out and I need this to be gradual and not instant as it would be with CSS.我需要图像不透明度在鼠标悬停时增加,鼠标移开时降低,我需要这是渐进的而不是像 CSS 那样即时的。

<html>
    <head>
    </head>
    <body>
        <img id="test" src="space1.png" onmouseover="updateOpacity()"/>
        
        <script type="text/javascript">
        
            var element = document.getElementById('test');
            var opacity = element.style.opacity;
            
            function updateOpacity()
            {
                opacity = opacity += 0.2;
                
                if(opacity >= 1)
                {
                    // done
                    opacity = 1;
                }
                else
                {
                    element.style.opacity = opacity; // this updates the opacity
                    setTimeout(function(){updateOpacity();}, 50); // 50 is the delay, 50milliseconds
                }
            }
        </stript>
    
    </body>
</html>

If you don't need to support current IE, you can do this cleanly using CSS transitions :如果您不需要支持当前的 IE,您可以使用CSS transitions干净地做到这一点:

HTML HTML

<img id="test" src="space1.png"/>

CSS CSS

#test {
    transition: opacity 600ms;
    opacity: 0.35;
}

#test:hover {
    opacity: 1.0;
}

Obligatory jsFiddle: http://jsfiddle.net/millimoose/uNeMg/强制性 jsFiddle: http : //jsfiddle.net/millimoose/uNeMg/

(For the sake of clarity and sanity I'm omitting the vendor prefixes and using -prefix-free .) (为了清晰起见,我省略了供应商前缀并使用-prefix-free 。)

Is jQuery an option? jQuery 是一种选择吗?

<img id="test" src="space1.png" style="opacity: 0.1;">

<script>
var delay = 500; //500ms
$("#test").mouseover(function(){
    $(this).stop().animate({
        opacity: 1.0
    }, delay);
}).mouseout(function(){
     $(this).stop().animate({
        opacity: 0.1
    },  delay); 
});
</script>


EDIT: http://jsfiddle.net/m3PAy/编辑: http : //jsfiddle.net/m3PAy/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:我有一个空的div正方形网格,当我将鼠标悬停在它们上方时,我需要鼠标事件来更改每个div的颜色 - Javascript: I have a grid of empty div squares I need mouse events to change the color of each div when I mouse over them 如何使用我的代码无法运行的jquery更改滚动类的背景颜色? - How can I change the background color of a class on scroll using jquery my code isn't working? 当我使用 fetch my.then 代码不起作用 - When I use fetch my .then code isn't working 我有一个简单的网站,想在鼠标悬停时更改字体大小,但此代码不起作用。为什么? - I have a simple site and want to change my font-size when the mouse is over but this code don't work.Why? 当我将鼠标悬停在图片上时,我希望它一直放大直到不再将鼠标放在上面 - When I mouse over an image, I want it to keep enlarging until I don't have my mouse on it anymore 重构我的JavaScript代码,我需要一种方法来防止鼠标滑过时文本重复出现? - Refactor my javascript code and I need a way how to prevent the text repeated when the mouse on? 当我将 JavaScript function 导入到我的 Z4C4DBAD5FCA2E78AAA47F 文件中时,如何修复无法工作的 onclick - How do I fix an onclick that isn't working when I import a JavaScript function into my HTML file 当我将其导入Wordpress网站时,为什么我的JQuery / CSS代码不起作用? - Why isn't my JQuery/CSS code working when I import it into my Wordpress website? 当我将鼠标放在 JavaScript 上时,我需要重新定位方块。 我怎么做? - I need to reposition the squares when I put the mouse over JavaScript. How do I do that? 使用javascript更改图像不透明度 - change image opacity using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM