为什么“($(this).css('opacity')==。3)不行? [英] Why does "($(this).css('opacity')==.3)" not work?

查看:320
本文介绍了为什么“($(this).css('opacity')==。3)不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在jquery中创建一个if语句,检查不透明度是否为.3,但不是
工作。

  if($(this).css('opacity')==。3){$(this).fadeTo(500,1);} 

我的格式化不透明度不正确吗?我也尝试过 0.3 无效。



背景: b
$ b

我正在使用淡入淡出效果,当一个对象在滚动过去时淡入淡出,向上滚动时淡出。



myfunkyside亲切地告诉我如何做到这一点:向下滚动,向上滚动向上滚动 - 基于窗口中的元素位置



myfunkyside的原始jsfiddle: http://jsfiddle.net/b7qnrsrz/1/



在这种情况下,淡入淡出从0不透明度变为1并返回。



我试图替换

  if(objectBottom< windowBottom){
if($(this).css('opacity')== 0){$(this).fadeTo(500,1);}
} else {//对象退出视图(向上滚动)
if($(this).css('opacity')== 1){$(this).fadeTo(500,0) ;}
}

  if(objectBottom< windowBottom){
if($(this).css('opacity')==。3){$(this).fadeTo ,1);}
} else {//对象退出视图(向上滚动)
if($(this).css('opacity')== 1){$(this)。 fadeTo(500,.3);}
}

这里是我对jsfiddle的尝试: http://jsfiddle.net/b7qnrsrz/3/

感谢您看看这个!

解决方案

或者因为你只处理两种情况(正/负测试),你可以使用 == 1 != 1 ,而不是 ==。3 。此外,它是 ,请务必使用 .stop()转换/动画。



http://jsfiddle.net/b7qnrsrz/16/


I am trying to create an if statement in jquery that checks to see if the opacity is .3, but it does not work.

if ($(this).css('opacity')==.3) {$(this).fadeTo(500,1);}

Am I formatting the opacity incorrectly? I also tried 0.3 to no avail.

Background:

I'm working with a fade effect, where an objects fades in when you scroll past it and fades out when you scroll up.

myfunkyside kindly showed me how to do this here: Fade In on Scroll Down, Fade Out on Scroll Up - based on element position in window

myfunkyside's original jsfiddle: http://jsfiddle.net/b7qnrsrz/1/

In this instance, the fade goes from 0 opacity to 1 and back. I want to go from .3 to 1 and back instead.

I tried to replace

if (objectBottom < windowBottom) {
    if ($(this).css('opacity')==0) {$(this).fadeTo(500,1);}
} else { //object goes out of view (scrolling up)
    if ($(this).css('opacity')==1) {$(this).fadeTo(500,0);}
}

with

if (objectBottom < windowBottom) {
    if ($(this).css('opacity')==.3) {$(this).fadeTo(500,1);}
} else { //object goes out of view (scrolling up)
    if ($(this).css('opacity')==1) {$(this).fadeTo(500,.3);}
}

but it does not work. Here's my attempt on jsfiddle: http://jsfiddle.net/b7qnrsrz/3/

Thanks for taking a look at this!

解决方案

You could do what everyone else is saying, or since you're only dealing with two case (positive/negative test), you could use ==1 and !=1, instead of ==.3. Also, it's jQuery, make sure to use .stop() with transitions/animations.

http://jsfiddle.net/b7qnrsrz/16/

这篇关于为什么“($(this).css('opacity')==。3)不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆