jQuery中的错误如果... Else语句

时间:2017-03-19 20:56:23

标签: jquery

有人可以在此代码中找到错误吗?当我使用这个破解的jQuery代码运行它时,我的整个网站,除了正文样式,都会消失。我的网站太大,无法发布整个html,css,jq代码...... 关于我想用以下代码做什么的更好代码的建议非常受欢迎。代码首先激活.Box1,.Box2的拖动...然后它动画移动到不同的位置。我只想在点击时切换.smartBox转到A点,然后再次点击回到B点。

提前致谢。



/* .smartBox on click: actiave dragging */
    var smartBoxClicked = false;
    
    
    if(smartBoxClicked = false) {
        
        $('.smartBox').click(function() {
            $('.Box1, .Box2, .Box3, .Box4').draggable();
            $(this).animate({'top': '1%'}, 1000).animate({'left': '48%'}, 1000);
            smartBoxClicked = true;
        } else {
            $('.smartBox').click(function() {
                $(this).animate({
                    'top': '370px'
                }, 1000).animate({'left': '32.26%'}, 1000);
                smartBoxClicked = false;
        });
    });
    };
    
    /* .smartBox is a div with an image */

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

应该是

var smartBoxClicked = false;    

if(smartBoxClicked == false) {
     $('.smartBox').click(function() {          
         $('.Box1, .Box2, .Box3, .Box4').draggable();
         $(this).animate({'top': '1%'}, 1000).animate({'left': '48%'}, 1000);
         smartBoxClicked = true;
     }); 
}else {
     $('.smartBox').click(function() {
        $(this).animate({
           'top': '370px'
        }, 1000).animate({'left': '32.26%'}, 1000);
        smartBoxClicked = false;
     });
};

“=”用于赋值,“==”应该用于比较,而且大括号也不匹配。

答案 1 :(得分:0)

在这种情况下使用切换类非常简单:

.animated{
   top: 1%;
   left: 48%;
 }
 .smartBox
 {
    transition-duration: 1s;
 }

和Jquery:

$('.smartBox').click(function() {
        $(this).toggleClass('animated');
}