简体   繁体   English

jQuery-第二次单击具有不同的功能

[英]jQuery - Different function on second click

I'm trying to create a curtain Opening & Closing effect. 我正在尝试创建一个窗帘打开和关闭效果。 But for some reason I can't make my curtains close once they're opened. 但是由于某种原因,窗帘一旦打开我就无法将其关闭。

Here's what I have so far: http://jsfiddle.net/ssc3z1tf/ 到目前为止,这是我所拥有的: http : //jsfiddle.net/ssc3z1tf/

I've tried quite a few different ways, adding and removing classes, toggling classes and here I've used rels. 我尝试了很多不同的方法,添加和删除类,切换类,在这里我使用了rels。

$(document).ready(function () {
             $('.curtain').click(function(){
                if ($('.curtain').attr('rel', 'open')){
                        $('.curtainLeft').animate({"left":"-400px"}, "slow");
                        $('.curtainRight').animate({"right":"-400px"}, "slow");
                        $('.curtain').attr('rel', 'closed');

                    } else if ($('.curtain').attr('rel', 'closed')){

                        $('.curtainLeft').animate({"left":"-0px"}, "slow");
                        $('.curtainRight').animate({"right":"-0px"}, "slow");
                        $('.curtain').attr('rel', 'open');
                    }
            });
        });

Any help would be great! 任何帮助将是巨大的! I'm lost! 我迷路了! Thanks!! 谢谢!!

Change 更改

if ($('.curtain').attr('rel', 'open')){

to

if ($('.curtain').attr('rel') == 'open') {

And do the same for the second if statement. 并对第二条if语句执行相同的操作。 The .attr(name, value) will always set the attribute to the specified value, whereas .attr(name) will simply return the value .attr(name, value)将始终将属性设置为指定的值,而.attr(name)将仅返回该值

Updated Fiddle 更新小提琴

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM