【问题标题】:Hugo - Shortcode - Modal windowHugo - 简码 - 模态窗口
【发布时间】:2021-01-12 07:53:59
【问题描述】:

我使用的是 Hugo 0.74 版

我已经建立了一个简码:

  1. 使用我指定的文本创建一个按钮
  2. 打开一个由 getform.io 组成的模式窗口

一切正常,预计会有一个小故障:

我必须按两次按钮才能打开模式窗口

我已经在 Chrome、Firefox 和 Edge 中尝试过这个,我需要点击两次才能打开它。

我哪里错了?

{{$text := .Get "text"}}


<button id="id_reachout" style="background:#28A745;color:white;font-family: 'Varela Round';" onClick="showModal();"> {{$text}}   </button>

<div id="myModal" class="modal">
    <!-- Modal content -->
    <div class="modal-content">
        <span class="close">&times;</span>
        <div class="row">
            <div class="modal-column" style="border: 5px double red">
                <img src="/img/MyModal.png" alt="Alternate text" class="modal-image">
            </div>
            <div class="modal-column">
                <form action="https://getform.io/f/4804d94b-8f1q-4a05-a679-7da0ba070952" method="POST">
                    <label for="id_name" class="modal-label"> Name </label>
                    <input type="text" name="name" id="id_name" class="modal-input">
                    <label for="id_email" class="modal-label"> Email </label>
                    <input type="email" name="email" id="id_email" class="modal-input">
                    <label for="id_message" class="modal-label"> Message </label>
                    <textarea name="message" id="id_message" class="modal-textarea"> </textarea>
                    <input id="id_submit" class="modal-submit" name="submit" type="submit" value="Submit">
                </form>
            </div>
        </div>
    </div>
</div>

在我的 MyArticle.md 文件中,我将此短代码称为:

{{<modal_button text="I have some more questions related to assets!">}}

我的 custom.js 文件是:

function showModal() {

    // Get the modal
    var modal = document.getElementById("myModal");

    // Get the button that opens the modal
    var btn = document.getElementById("id_reachout");

    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close")[0];

    // When the user clicks on the button, open the modal
    btn.onclick = function () {
        modal.style.display = "block";
    }

    // When the user clicks on <span> (x), close the modal
    span.onclick = function () {
        modal.style.display = "none";
    }

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function (event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
}

最后这是我的 css 文件:

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    display:block;
    background-color: #fefefe;
    margin: 5% auto; /* 5% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
    height: 80%;
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

    .close:hover,
    .close:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }

.modal-label {
    display:block;
    margin-top:20px;
    letter-spacing: 2px;
    font-weight:bold;
}

.modal-input {
    width: 439px;
    height: 27px;
    background: #efefef;
    border: 1px solid #dedede;
    padding: 10px;
    margin-top: 3px;
    font-size: 0.9em;
    color: #3a3a3a;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}

    .modal-input:focus {
        border: 2px solid #5DADE2;
    }

.modal-textarea {
    width: 439px;
    height: 200px;
    background: #efefef;
    border: 1px solid #dedede;
    padding: 10px;
    margin-top: 3px;
    font-size: 0.9em;
    color: #3a3a3a;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}

.modal-submit {
    width: 439px;
    height: 35px;
    background: #efefef;
    border: 1px solid #dedede;
    font-size: 0.9em;
    color: #3a3a3a;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}

#submit {
    width: 127px;
    height: 60px;
    text-indent: -9999px;
    border: none;
    cursor: pointer;
}
    #submit:hover {
        opacity: .9;
    }

.row {
    display: flex;
}

.modal-column {
    flex:50%;
    padding:10px;
}

.left-column{
    font-size:2rem;

}

【问题讨论】:

    标签: javascript html css markdown hugo


    【解决方案1】:

    当用户单击按钮时,您正在调用showModal()。然而,该函数添加了 另一个 onclick = function - 这需要用户点击两次。

    showModal() 应立即调用显示模式的逻辑,而不是附加另一个 onclick 函数。

    快速修复:

    替换:

    
    btn.onclick = function () {
        modal.style.display = "block";
    }
    

    与:

    if (modal.style.display != "block") {
       modal.style.display = "block";
    }
    
    

    Demo - 如果需要,您可以省略 if 检查,如果需要,只需将 onclick 替换为 modal.style.display = "block";

    function showModal() {
    
        // Get the modal
        var modal = document.getElementById("myModal");
    
        // Get the button that opens the modal
        var btn = document.getElementById("id_reachout");
    
        // Get the <span> element that closes the modal
        var span = document.getElementsByClassName("close")[0];
        
        if (modal.style.display != "block") {
            modal.style.display = "block";
        }
    
        // When the user clicks on <span> (x), close the modal
        span.onclick = function () {
            modal.style.display = "none";
        }
    
        // When the user clicks anywhere outside of the modal, close it
        window.onclick = function (event) {
            if (event.target == modal) {
                modal.style.display = "none";
            }
        }
    }
    /* The Modal (background) */
    .modal {
        display: none; /* Hidden by default */
        position: fixed; /* Stay in place */
        z-index: 1; /* Sit on top */
        left: 0;
        top: 0;
        width: 100%; /* Full width */
        height: 100%; /* Full height */
        overflow: auto; /* Enable scroll if needed */
        background-color: rgb(0,0,0); /* Fallback color */
        background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    }
    
    /* Modal Content/Box */
    .modal-content {
        display:block;
        background-color: #fefefe;
        margin: 5% auto; /* 5% from the top and centered */
        padding: 20px;
        border: 1px solid #888;
        width: 80%; /* Could be more or less, depending on screen size */
        height: 80%;
    }
    
    /* The Close Button */
    .close {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }
    
        .close:hover,
        .close:focus {
            color: black;
            text-decoration: none;
            cursor: pointer;
        }
    
    .modal-label {
        display:block;
        margin-top:20px;
        letter-spacing: 2px;
        font-weight:bold;
    }
    
    .modal-input {
        width: 439px;
        height: 27px;
        background: #efefef;
        border: 1px solid #dedede;
        padding: 10px;
        margin-top: 3px;
        font-size: 0.9em;
        color: #3a3a3a;
        -moz-border-radius: 5px;
        -webkit-border-radius: 5px;
        border-radius: 5px;
    }
    
        .modal-input:focus {
            border: 2px solid #5DADE2;
        }
    
    .modal-textarea {
        width: 439px;
        height: 200px;
        background: #efefef;
        border: 1px solid #dedede;
        padding: 10px;
        margin-top: 3px;
        font-size: 0.9em;
        color: #3a3a3a;
        -moz-border-radius: 5px;
        -webkit-border-radius: 5px;
        border-radius: 5px;
    }
    
    .modal-submit {
        width: 439px;
        height: 35px;
        background: #efefef;
        border: 1px solid #dedede;
        font-size: 0.9em;
        color: #3a3a3a;
        -moz-border-radius: 5px;
        -webkit-border-radius: 5px;
        border-radius: 5px;
    }
    
    #submit {
        width: 127px;
        height: 60px;
        text-indent: -9999px;
        border: none;
        cursor: pointer;
    }
        #submit:hover {
            opacity: .9;
        }
    
    .row {
        display: flex;
    }
    
    .modal-column {
        flex:50%;
        padding:10px;
    }
    
    .left-column{
        font-size:2rem;
    
    }
    <button id="id_reachout" style="background:#28A745;color:white;font-family: 'Varela Round';" onClick="showModal();"> {{$text}}   </button>
    
    <div id="myModal" class="modal">
        <!-- Modal content -->
        <div class="modal-content">
            <span class="close">&times;</span>
            <div class="row">
                <div class="modal-column" style="border: 5px double red">
                    <img src="/img/MyModal.png" alt="Alternate text" class="modal-image">
                </div>
                <div class="modal-column">
                    <form action="https://getform.io/f/4804d94b-8f1q-4a05-a679-7da0ba070952" method="POST">
                        <label for="id_name" class="modal-label"> Name </label>
                        <input type="text" name="name" id="id_name" class="modal-input">
                        <label for="id_email" class="modal-label"> Email </label>
                        <input type="email" name="email" id="id_email" class="modal-input">
                        <label for="id_message" class="modal-label"> Message </label>
                        <textarea name="message" id="id_message" class="modal-textarea"> </textarea>
                        <input id="id_submit" class="modal-submit" name="submit" type="submit" value="Submit">
                    </form>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 您好贾斯汀,感谢您的快速回复。它帮助我朝着正确的方向思考。我进行了以下更改: 1. 删除了 showModal() 中的无关代码 2. 添加了关闭窗口功能。我完全尝试了您的代码,但它不起作用。
    • @Mani 很高兴听到你想通了 - 是的,主要问题是在点击后添加 onclick - 我很惊讶我建议的代码更改不起作用,它起作用了演示。也许你的本地机器上有一些代码在演示中没有出现
    【解决方案2】:

    感谢贾斯汀。它为我指明了正确的方向,下面的代码解决了这个问题:

    function showModal() {
    
        // Get the modal
        var modal = document.getElementById("myModal");
    
        modal.style.display = "block";
    }
    
    /* Added a close Modal */
    
    function closeModal() {
    
        // Get the <span> element that closes the modal
        var modal = document.getElementById("myModal");
        modal.style.display = "none";
    
    }
    
    

    在 html 中,我只对 span 做了一个小改动 - 我添加了一个 onClick=closeModal() :

    <div id="myModal" class="modal">
        <!-- Modal content -->
        <div class="modal-content">
            <span class="close" onclick="closeModal();">&times;</span>
            <div class="row">
                <div class="modal-column" style="border: 5px double red">
                    <img src="/img/PlanyourmoneyModal.png" alt="Financial Planning, Goal Planning" class="modal-image">
                </div>
                <div class="modal-column">
                    <form action="https://getform.io/f/4804d94b-8f1b-4a05-a679-7da0ba070952" method="POST">
                        <label for="id_name" class="modal-label"> Name </label>
                        <input type="text" name="name" id="id_name" class="modal-input">
                        <label for="id_email" class="modal-label"> Email </label>
                        <input type="email" name="email" id="id_email" class="modal-input">
                        <label for="id_message" class="modal-label"> Message </label>
                        <textarea name="message" id="id_message" class="modal-textarea"> </textarea>
                        <input id="id_submit" class="modal-submit" name="submit" type="submit" value="Submit">
                    </form>
                </div>
            </div>
        </div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-13
      • 2012-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多