gpt4 book ai didi

javascript - 如何使用淡入淡出正确地在 DIV 之间切换?

转载 作者:行者123 更新时间:2023-12-03 01:30:03 26 4
gpt4 key购买 nike

我正在尝试制作一个示例网站,但我被困在按钮可以切换哪些 div 淡入和淡出的部分。编辑:hev1 修复了主要问题,但还有其他一些问题,所以我在这里修复了其他问题。

    <script>
var activebutton = "None";
var finished = true;
var buyInfoShown = false;
var rentInfoShown = false;
function BuyInfo() {
if(!buyInfoShown && finished === true) {
finished = false;
$("#RentInfo").fadeOut("slow", function() {
$("#BuyInfo").fadeIn("slow")
buyInfoShown = true;
rentInfoShown = false;
});
}
if(buyInfoShown && finished === true) {
finished = false;
$("#BuyInfo").fadeOut("slow");
buyInfoShown = false;
}
finished = true;
}
function RentInfo() {
if(!rentInfoShown && finished === true) {
finished = false;
$("#BuyInfo").fadeOut("slow", function() {
$("#RentInfo").fadeIn("slow");
rentInfoShown = true;
buyInfoShown = false;
});
}
if(rentInfoShown && finished === true) {
finished = false;
$("#RentInfo").fadeOut("slow");
rentInfoShown = false;
}
finished = true;
}
</script>

最佳答案

您需要两个不同的变量来分别检查 #BuyInfo#RentInfo 的可见性。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var activebutton = "None";
var finished = false;
var buyInfoShown = false;
var rentInfoShown = false;
function BuyInfo() {
if(!buyInfoShown && finished === false) {
$("#BuyInfo").fadeIn("slow");
$("#RentInfo").fadeOut("slow");
buyInfoShown = true;
rentInfoShown = false;
finished = true;
}
if(buyInfoShown && finished === false) {
$("#BuyInfo").fadeOut("slow");
buyInfoShown = false;
finished = true;
}
finished = false;
}
function RentInfo() {
if(!rentInfoShown && finished === false) {
$("#RentInfo").fadeIn("slow");
$("#BuyInfo").fadeOut("slow");
rentInfoShown = true;
buyInfoShown = false;
finished = true;
}
if(rentInfoShown && finished === false) {
$("#RentInfo").fadeOut("slow");
rentInfoShown = false;
finished = true;
}
finished = false;
}
</script>
...
<div class="main" id="pricing">
<p style="font-size: 28px;">Pricing</p>
<div class="btn-group">
<button onclick="BuyInfo()" type="button" class="btn btn-outline-primary btn-lg">Buy</button>
<button onclick="RentInfo()" type="button" class="btn btn-outline-primary btn-lg">Rent</button>
</div>
<div id="BuyInfo" style="display: none;">
<p style="font-size: 18px;">Product</p>
<p style="font-size: 14px;">Price</p>
<p>(Image)</p>
<a href="link"><button type="button" class="btn btn-success btn-lg">Buy It Now!</button></a>
<p style="font-size: 20px;"><b>Use code "code" (without quotes) to get a $20 discount!</b></p>
</div>
<div id="RentInfo" style="display: none;">
<p>Rent Info</p>
</div>
<p style="height: 120px;"></p>
<div style="height: 80px; padding: 80px 0px;">
<p style="font-size: 12px;">Created by HinkerThinker</p>
</div>
</div>

关于javascript - 如何使用淡入淡出正确地在 DIV 之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51343941/

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