简体   繁体   English

div切换不起作用我想要它

[英]div toggling not working how i want it to

please take a look at my jsfiddle: http://jsfiddle.net/e8hj27gf/21/ 请看看我的jsfiddle: http : //jsfiddle.net/e8hj27gf/21/

You can see i have a display and hide function, How can i toggle this on and off, using one click event? 您可以看到我具有显示和隐藏功能,如何使用一键事件将其打开和关闭? please keep in mind i require the functionality of being able to close the div if the user clicks outside of it intact. 请记住,如果用户完整点击div,我需要能够关闭div的功能。

My Javascript: 我的Javascript:

    $(document).ready(function () {

      var container = $("#boxwrapper");

       $("#toggleon").click(function (e) {

            if (!container.is(':visible'))
                Display();
       });

       $("#toggleoff").click(function (e) {

            if (container.is(':visible'))
                Hide();
       });



       $(document).mouseup(function (e) {

           if (!container.is(e.target) && container.has(e.target).length === 0) {
                if (container.is(':visible'))
                    Hide();
             }

        });                       
    });

    function Display() {
            $("#boxwrapper").show();
            $("#boxwrapper").addClass("box");              
        }

   function Hide() {
            $("#boxwrapper").hide();           
   }    

I hope that makes sense! 我希望这是有道理的!

Answer: http://jsfiddle.net/e8hj27gf/25/ 答案: http//jsfiddle.net/e8hj27gf/25/

JavaScript_ JavaScript_

    $(document).ready(function () {
        $("#toggle").click(function(){
             $("#boxwrapper").fadeToggle("slow");  
        });       
    });

CSS CSS

#boxwrapper
    {
        width: 100%;
        margin: auto;
        top: 0px;
        left: 20%;
        right: 0px;
        bottom: 0px;
        background-color: white;
        opacity: 0.4;
        position: fixed;
        overflow-y: scroll;
        overflow-x: scroll;
        display:none;
    }

HTML HTML

    <div id="main">
        <a href='#' id='toggle'>Toggle</a>
    </div>

    <div id="boxwrapper">
    </div>

Search up the jQuery library, specifically toggle().. fadeToggle() , slideToggle() etc. etc. 搜索jQuery库,特别是toggle().. fadeToggle()slideToggle()等。

I think what you want to use is the jQuery .toggle() method. 我认为您要使用的是jQuery .toggle()方法。

See: http://api.jquery.com/toggle/ 请参阅: http//api.jquery.com/toggle/

eg: 例如:

function ShowHide() {
        $("#boxwrapper").toggle();
        $("#boxwrapper").addClass("box");              
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM