2016-10-04 86 views
0

我有下面的代码。我希望它能够将div制作成一个计算好的位置,但某些事情是不可能的,或者我犯了一个错误。任何人都可以帮忙吗?切换div来计算位置

$(document).ready(function() { 
 
     var is_Clicked = false; 
 
     $("#togglebutton").click(function() { 
 
     if (is_Clicked) { 
 
      $('#myToggleDiv').css('float','left'); 
 
      $("#myToggleDiv").animate({ 
 
      left: '0%' 
 
      }); 
 
      is_Clicked = false; 
 
     } else { 
 
      $('#myToggleDiv').css('float','right'); 
 
      $("#myToggleDiv").animate({ 
 
       $('#myToggleDiv').css('right','calc(100vw - 40px)); 
 
      }); 
 
      is_Clicked = true; 
 
     } 
 
     }); 
 
    }); 
 

 
    $(document).ready(function() { 
 
     document.getElementById("myText").innerHTML = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br><br>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; 
 
    });
html, 
 
body { 
 
    width: 100%; 
 
    height: 100%; 
 
} 
 

 
#header { 
 
    width: 100%; 
 
    height: 20%; 
 
    float: left; 
 
    border: 1px solid; 
 
} 
 

 
#myToggleDiv { 
 
position: relative; 
 
    border: 1px solid; 
 
} 
 

 
#togglebutton { 
 
    width: 10%; 
 
    height: 40%; 
 
    margin-right: 5px; 
 
    margin-top: 5px; 
 
    float: right; 
 
} 
 

 
@media screen and (min-width: 1366px) { 
 
    #myToggleDiv { 
 
    width: 60vw; 
 
    } 
 
} 
 

 
@media screen and (max-width: 1365px) { 
 
    #myToggleDiv { 
 
    width: 70vw; 
 
    } 
 
} 
 

 
@media screen and (max-width: 1024px) { 
 
    #myToggleDiv { 
 
    width: 90vw; 
 
    } 
 
} 
 

 
@media screen and (max-width: 800px) { 
 
    #myToggleDiv { 
 
    width: 100vw; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="myToggleDiv"> 
 

 
    <input type="button" value="Toggle" id="togglebutton"> 
 
    <p id="myText"></p> 
 
</div>

+0

你'有生的语法()'调用是错误的 - 你需要提供一个有效的对象: http://jsfiddle.net/100pvu95/37/。这就是说,你有一个更大的问题,因为你无法为计算的CSS值设置动画,所以即使使用了正确的语法,你所做的也是行不通的。 –

+0

为什么在问题编辑器**中有小提琴工具**时连接到外部小提琴? – evolutionxbox

+0

你把**'$('#myToggleDiv').css('right','calc(100vw - 40px)); **在**对象**内? –

回答

0

这是因为.animate期待的对象。所以,改变你这样的代码:

$("#myToggleDiv").animate({ 
     'right':'calc(100vw - 40px)' 
}); 

要留意把'calc(100vw - 40px)'内引用

http://jsfiddle.net/100pvu95/40/

+0

谢谢!请看Rory McCrossan的回答。是否可以将('right','calc(100vw - 40px))值转换为var并使切换生效? – Eddy

+0

我的不好。忘记注意了。不,你不能这样做,因为那是一个字符串。但是,你当然可以这样做: var something = 5 + 6; $( “#myToggleDiv”)动画({ \t \t \t \t \t \t \t \t '正确':东西 })。 http://jsfiddle.net/100pvu95/41/ –

+0

谢谢你把我推向正确的方向,但仍然有两个问题。请看这里:http://stackoverflow.com/questions/39849785/keep-toggled-div-in-position – Eddy