jQuery:你能用 jQuery 找到所选元素的不透明度吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2972774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Can you find the selected element's opacity with jQuery?
提问by Steckel
I have a filter running on a set of list elements which fades the lesser desirable elements down to 0.25 opacity but I'd love to have their opacity return to 1 and then back down to 0.25 on hover over and out. Is this fairly simple to do?
我有一个在一组列表元素上运行的过滤器,它将不太理想的元素淡化到 0.25 不透明度,但我希望它们的不透明度返回到 1,然后在悬停时返回到 0.25。这很简单吗?
I'm only having trouble finding a way to grab the selected element's current opacity so I can store it in a variable for use.
我只是找不到一种方法来获取所选元素的当前不透明度,以便我可以将其存储在变量中以供使用。
$('#centerPanel li').hover(function(){
var currentOpacity = $(this).?????
$(this).fadeTo(1,1);
},
function(){
$(this).fadeTo(1,currentOpacity);
});
回答by dirt
there are complete guide "Get Current Opacity in MSIE using jQuery" http://zenverse.net/get-current-opacity-in-msie-using-jquery-cross-browser-codes/
有完整的指南“使用 jQuery 在 MSIE 中获取当前不透明度” http://zenverse.net/get-current-opacity-in-msie-using-jquery-cross-browser-codes/
code:
代码:
function getopacity(elem) {
var ori = $(elem).css('opacity');
var ori2 = $(elem).css('filter');
if (ori2) {
ori2 = parseInt( ori2.replace(')','').replace('alpha(opacity=','') ) / 100;
if (!isNaN(ori2) && ori2 != '') {
ori = ori2;
}
}
return ori;
}
//to use it
var currentopacity = getopacity('div.the-element');
回答by jAndy
$('#centerPanel li').hover(function(){
if(!$(this).is(':animated'))
$(this).animate({opacity: 'toggle'}, 1000);
},
function(){
if(!$(this).is(':animated'))
$(this).animate({opacity: 'toggle'}, 1000);
});
回答by cusspvz
You need to set the mouseout opacity var outside the function, this will prevent your function to change that value.
您需要在函数外部设置 mouseout opacity var,这将阻止您的函数更改该值。
nohoverOpacity = $('#centerPanel li').css("opacity");
hoverOpacity = 1;
dur = 1000;
$('#centerPanel li').hover(function(){
$(this).fadeTo(dur,hoverOpacity);
},function(){
$(this).fadeTo(dur,nohoverOpacity);
});
Is this what you want? :)
这是你想要的吗?:)