2017-03-19 35 views
-2

有人可以找到此代码中的错误?当我用这个破碎的jQuery代码运行它时,除了正文样式,我的整个网站都消失了。我的网站太大,无法发布整个html,css,jq代码... 对于我想要用下面的代码进行更好的代码的建议非常受欢迎。代码首先激活拖动.Box1,.Box2 ...然后它动画移动到不同的位置。我只是想要切换.smartBox点击时去点A,当点击时再点B点。jQuery中的错误如果...其他语句

在此先感谢。

/* .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>

+1

有很多语法错误的 – NineBerry

+1

你的大括号不匹配,在所有 – Psi

+0

'= '赋值,'=='验证值并且可以与不同的数据类型一起工作,'==='严格值验证不适用于不同的数据类型 – Baruch

回答

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; 
    }); 
}; 

“=” 是赋值, “==” 应该是比较,也是你的大括号不匹配。

+0

不能相信我没有看到......:D谢谢 – Cass

0

在使用肘类此情况下,是很容易的:

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

和jQuery:

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

谢谢你的想法,我甚至可能会切换到toggleClass =] – Cass

+0

欢迎您。如果你想继续你的方式,你不需要'if(smartBoxClicked == false)'。它更快,更干净:'if(!smartBoxClicked)'。 –