gpt4 book ai didi

javascript - 将 div 切换到计算位置

转载 作者:行者123 更新时间:2023-11-28 05:05:32 25 4
gpt4 key购买 nike

我有下面的代码。我希望它能将一个 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>

最佳答案

那是因为 .animate 需要一个对象。所以像这样改变你的代码:

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

注意将 'calc(100vw - 40px)' 放在引号内

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

关于javascript - 将 div 切换到计算位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39847741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com