2016-10-04 77 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