簡體   English   中英

隱藏div點擊外面

[英]Hide div click on outside

我想隱藏hidden的 class div。 當用戶點擊 div 外部時, hidden的 div 應該是滑出的。

HTML

<!DOCTYPE html>
<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   </head>
   <body>
      <div class="hidden">Here I am !</div>
      <div class="left">Left panel</div>
      <div class="right">Right panel</div>
      <div class="clear"></div>
      <a href="#" id="slide">Show/hide Slide Panel</a>
   </body>
</html>

CSS

.left,
.hidden {
    float: left;
    height: 350px;
}

.left {
    width: 50%;
    background: #fff;
    z-index: 1;
}

.hidden {
    width: 25%;
    z-index: 2;
    position: absolute;
    left: -1000px;
    background: #f90;
    color: #000;
}

.right {
    width: 50%;
    float: right;
    height: 350px;
    background: #000;
    color: #fff;
}

.clear {
    clear: both;
}

JS

$(document).ready(function () {
    $('#slide').click(function () {
        var hidden = $('.hidden');
        if (hidden.hasClass('visible')) {
            hidden.animate({
                "left": "-1000px"
            }, "slow").removeClass('visible');
        } else {
            hidden.animate({
                "left": "0px"
            }, "slow").addClass('visible');
        }
    });
});

提前致謝!

你可以你這個代碼。 您可以定義頁面的哪些元素可以隱藏您的面板。 我在這里選擇leftrightclear類:

 $(document).ready(function () { $('#slide').click(function () { hide_el (); }); $('.left, .right, .clear').click(()=>{ var hidden = $('.hidden'); if (hidden.hasClass('visible')) { hidden.animate({ "left": "-1000px" }, "slow").removeClass('visible'); } }); }); function hide_el (){ var hidden = $('.hidden'); if (hidden.hasClass('visible')) { hidden.animate({ "left": "-1000px" }, "slow").removeClass('visible'); } else { hidden.animate({ "left": "0px" }, "slow").addClass('visible'); } }
 .left, .hidden { float: left; height: 350px; }.left { width: 50%; background: #fff; z-index: 1; }.hidden { width: 25%; z-index: 2; position: absolute; left: -1000px; background: #f90; color: #000; }.right { width: 50%; float: right; height: 350px; background: #000; color: #fff; }.clear { clear: both; }
 <:DOCTYPE html> <html> <head> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </head> <body> <div class="hidden">Here I am !</div> <div class="left">Left panel</div> <div class="right">Right panel</div> <div class="clear"></div> <a href="#" id="slide">Show/hide Slide Panel</a> </body> </html>


如果您需要點擊正文,請使用以下代碼:

 $(document).ready(function () { $('#slide').click(function () { hide_el (); }); }); function body_click(){ setTimeout(()=>{ var hidden = $('.hidden'); $('body').click( function(event) { if( $(event.target).closest(hidden).length === 0 ) { if (hidden.hasClass('visible')) { hidden.animate({ "left": "-1000px" }, "slow").removeClass('visible'); }; } }); }, 1000); } function hide_el (){ $('body').unbind('click'); var hidden = $('.hidden'); if (hidden.hasClass('visible')) { hidden.animate({ "left": "-1000px" }, "slow").removeClass('visible'); } else { hidden.animate({ "left": "0px" }, "slow").addClass('visible'); body_click(); } }
 body { height: 300px; }.left, .hidden { float: left; height: 350px; }.left { width: 50%; background: #fff; z-index: 1; }.hidden { width: 25%; z-index: 2; position: absolute; left: -1000px; background: #f90; color: #000; }.right { width: 50%; float: right; height: 350px; background: #000; color: #fff; }.clear { clear: both; }
 <:DOCTYPE html> <html> <head> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </head> <body> <div class="hidden">Here I am !</div> <div class="left">Left panel</div> <div class="right">Right panel</div> <div class="clear"></div> <a href="#" id="slide">Show/hide Slide Panel</a> </body> </html>

以下代碼檢查您是否在該元素的后代的目標元素內單擊。 如果不是,將運行所需的代碼。 我沒有添加 animation 位,僅此而已。 這是小提琴https://jsfiddle.net/jhe36dmb/1/

$(document).mouseup(function (e)
 {
    var container = $('.hidden');

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {

      $('.hidden').css('left', '0');
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM