javascript - 如何在 jQuery mouseout 上应用原始样式

标签 javascript jquery html

我在背景图像上应用了淡出效果。如何在 mouseout 上缓慢应用回原始样式?

jsfiddle:http://jsfiddle.net/KevinOrin/H6Q3J/

jQuery('.square-section').mouseover(function(){
    jQuery(this).fadeTo('slow',0.3, function(){
        jQuery(this).css('background-image', 'url(' + $img + ')');
    }).fadeTo('slow',1);
});

最佳答案

您首先没有使用 $img 变量 ..所以首先不需要回调函数..

如果您要完全更改图像,回调函数可能会在此处提供帮助。

jQuery('.square-section').hover(function(){
    jQuery(this).fadeTo('slow',0.3);
}, function() {
    jQuery(this).fadeTo('slow',1);
});

Check Fiddle

如果你想交换 2 个不同的图像你可以试试这个方法

jQuery('.square-section').hover(function(){
    jQuery(this).fadeTo('slow', 0.3, function() {
        jQuery('.square', this).removeClass('square-chess').addClass('square-chart');
        jQuery(this).fadeTo('fast', 1);
    });
}, function() {
    jQuery(this).fadeTo('fast', 0.3, function() {
       jQuery('.square', this).removeClass('square-chart').addClass('square-chess');
       jQuery(this).fadeTo('fast', 1);
    });
});

Fiddle with 2 images

jQuery('.square-section').hover(function () {
    jQuery('.square', this).removeClass('square-chess').addClass('square-chart');
}, function () {
    jQuery('.square', this).removeClass('square-chart').addClass('square-chess');
});

关于javascript - 如何在 jQuery mouseout 上应用原始样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16551431/

相关文章:

jQuery 单击事件仍在过滤元素上触发

javascript - 在数组中搜索对象属性,然后隐藏第一次出现的找到的对象

html - CSS 让 float 填充未使用的空间

JavaScript 正则表达式解析 Latex\frac

javascript - jQuery promise 不使用方法 html undefined

javascript - 根据内容使用动态宽度的 Bootstrap 弹出窗口

jquery - 使用隐藏字段防止通过表单提交垃圾邮件

php 到 mysql 更新帖子不起作用

css - 如何减少最小顶部填充?

javascript - Selenium 不会点击最上面的元素