简体   繁体   English

仅使div的折叠部分可点击-展开部分应保持不可点击状态

[英]Make only collapsed portion of div clickable - expanded section should remain unclickable

I've got a nested div that contains 'additional information'. 我有一个嵌套的div,其中包含“其他信息”。

When a person clicks the div, it opens the 'additional information' div. 当某人单击div时,它将打开“其他信息” div。

I have used .stopPropagation() to prevent it from closing when the person clicks the text in the 'additional information', but if they click the borders or something, the collapse happens and the information goes away. 我使用.stopPropagation()来防止当用户单击“其他信息”中的文本时将其关闭,但是如果他们单击边框或其他内容,则会发生折叠并且信息消失。

I found this SO post - which was seemingly close to what I wanted, but I couldn't figure out how to make it work exactly how I need. 我找到了这则帖子 -似乎与我想要的内容很接近,但我不知道如何使它完全按我的需要工作。

Here is a codepen of what I'm trying to accomplish. 这是我要完成的工作的代码。

 $('#collapsDiv').on('show.bs.collapse', function( event ) { event.stopPropagation(); console.log('Only happen when outter div is opened'); }) $('#collapseDiv').on('hide.bs.collapse', function( event ) { event.stopPropagation(); console.log('Only happen when outter div is collapsed'); }) $('#additionalDiv').on('click', function( event ) { event.stopPropagation(); }) $('#collapseDiv').click(function(e) { if (e.pageY > $(this).offset().top + $(this).height()) { // bottom was clicked console.log('bottom clicked!'); e.stopPropagation(); } else { // up of the div was clicked console.log('top clicked'); } }); 
 #collapseDiv { cursor:pointer; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div id="collapseDiv" class="alert alert-warning" data-toggle="collapse" href="#additionalDiv" role="button" aria-expanded="false" aria-controls="additionalDiv"> This Div shows by default - I used an alert to make it more visually obvious. Only this part should remain clickable. The hidden stuff should not be clickable. A bonus would be having the lower portion not have a pointer! <div id="additionalDiv" class="collapse"> <div class="other-stuff"> <h2>This stuff should not be clickable</h2> <p>There may be paragraphs here a user might want to copy/paste!</p> <p>In a perfect world - even the bottom / sides would not trigger the collapse. This would only be collapsed by clicking the original div area.</p> </div> </div> </div> </div> 

This is 'working', but it only avoids clicks on the bottom - how would I make it avoid clicks on the sides as well? 这是“有效”的方法,但是它只能避免在底部发出咔嗒声,我又如何避免在侧面产生咔嗒声呢?

side note: it seems there is about a very tiny row on the bottom that causes it to collapse if clicked still. 旁注:似乎在底部大约有一个很小的行,如果仍然单击它会使其崩溃。

The padding space makes it clickable. 填充空间使其可单击。

You don't need javascript to achieve this. 您不需要javascript即可实现此目的。 Just add another div for toggling only . 只需添加另一个div 用于切换即可。

 #collapseDiv { padding: 0; } .my-toggle { cursor: pointer; padding: 20px; } #additionalDiv { padding: 20px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div id="collapseDiv" class="alert alert-warning"> <div class="my-toggle" data-toggle="collapse" href="#additionalDiv" role="button" aria-expanded="false" aria-controls="additionalDiv"> This Div shows by default - I used an alert to make it more visually obvious. Only this part should remain clickable. The hidden stuff should not be clickable. A bonus would be having the lower portion not have a pointer! </div> <div id="additionalDiv" class="collapse"> <div class="other-stuff"> <h2>This stuff should not be clickable</h2> <p>There may be paragraphs here a user might want to copy/paste!</p> <p>In a perfect world - even the bottom / sides would not trigger the collapse. This would only be collapsed by clicking the original div area.</p> </div> </div> </div> </div> 

I would adjust a couple things - get the additional div outside of the clickable one and put the padding on the sub-containers and styling on the container. 我会调整几件事-在可点击的内容之外获得额外的div,然后将填充物放在子容器上,并将样式放在容器上。

The problem really is that your container padding is clickable around the collapsable div. 问题确实是您的容器填充在可折叠div周围是可单击的。

.container--nopad {
    padding: 0;
}

#collapseDiv, #additionalDiv {
    padding: 20px;
}


<div class="container container--nopad alert alert-warning">
    <div id="collapseDiv"  data-toggle="collapse" href="#additionalDiv" role="button" aria-expanded="false" aria-controls="additionalDiv">
        This Div shows by default - I used an alert to make it more visually obvious. Only this part should remain clickable. The hidden stuff should not be clickable. A bonus would be having the lower portion not have a pointer!
    </div>
    <div id="additionalDiv" class="collapse">
        <div class="other-stuff">
            <h2>This stuff should not be clickable</h2>
            <p>There may be paragraphs here a user might want to copy/paste!</p>
            <p>In a perfect world - even the bottom / sides would not trigger the collapse. This would only be collapsed by clicking the original div area.</p>
        </div>
    </div>
</div>

codepen: https://codepen.io/dmgig/pen/EpzGLG codepen: https ://codepen.io/dmgig/pen/EpzGLG

the padding from the container div is what is causing the action. 来自容器div的填充是引起该操作的原因。 remove the padding and instead add it to both inner elements. 删除填充,而是将其添加到两个内部元素中。

 $('#collapsDiv').on('show.bs.collapse', function( event ) { event.stopPropagation(); console.log('Only happen when outter div is opened'); }) $('#collapseDiv').on('hide.bs.collapse', function( event ) { event.stopPropagation(); console.log('Only happen when outter div is collapsed'); }) $('#additionalDiv').on('click', function( event ) { event.stopPropagation(); }) $('#collapseDiv').click(function(e) { if (e.pageY > $(this).offset().top + $(this).height()) { // bottom was clicked console.log('bottom clicked!'); e.stopPropagation(); } else { // up of the div was clicked console.log('top clicked'); } }); 
  #collapseDiv { cursor:pointer; padding-left: 0; padding-right: 0; } #collapseDiv .triggerDiv, #collapseDiv #additionalDiv { padding-left: 1.25rem; padding-right: 1.25rem; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div id="collapseDiv" class="alert alert-warning" data-toggle="collapse" href="#additionalDiv" role="button" aria-expanded="false" aria-controls="additionalDiv"> <div class="triggerDiv">This Div shows by default - I used an alert to make it more visually obvious. Only this part should remain clickable. The hidden stuff should not be clickable. A bonus would be having the lower portion not have a pointer!</div> <div id="additionalDiv" class="collapse"> <div class="other-stuff"> <h2>This stuff should not be clickable</h2> <p>There may be paragraphs here a user might want to copy/paste!</p> <p>In a perfect world - even the bottom / sides would not trigger the collapse. This would only be collapsed by clicking the original div area.</p> </div> </div> </div> </div> 

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

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