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

标签 javascript html

我正在尝试制作一个示例网站,但我被困在按钮可以切换哪些 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/

相关文章:

javascript - 一次转换 2 个字符的十六进制字符串

javascript - jQuery:具有特殊字符的目标属性

javascript - 通过 JavaScript 为 CSS 网格生成单元格

html - 如何获取 HTML 文件中使用的 Css 类列表?

javascript - 使用替代 id 和名称以 PHP 形式生成附加行

html - 根据联系人制定的段落

javascript - ajax post请求中传递第二个参数

html - 如何在全宽表格中以最大宽度包装表格单元格

javascript - 如果将 addEventListener 放在 head 部分而不是 body 中,则 addEventListener 不起作用

javascript - 使用右/左动画隐藏/显示动态生成的表格列