简体   繁体   English

jQuery切换和单击事件上的slidetoggle切换后,元素具有怪异的弹跳

[英]jQuery toggle and slidetoggle on click event toggles the element with weird bounce after toggle

I am new to jQuery and JavaScript. 我是jQuery和JavaScript的新手。 I have list of elements which I set to display: none Upon click on the certain element, the list of elements should toggle down. 我设置了要display: none的元素列表display: none单击某个元素时,元素列表应向下切换。 It toggles down but the problem is after toggling down it bounces. 它向下切换,但是问题是在向下切换后它会反弹。

<div class="category">
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
     <h1 class="categoryHeading">Transportation</h1>
    <div class="items">
      <p class="item">MVV</p>
      <p class="item">Bike</p>
    </div>
  </div>

I read on the stackoverflow that it might be because of click event bubbles up the DOM and gets triggered multiple times. 我在stackoverflow上读到,这可能是因为click事件使DOM冒泡并多次触发。 So I have tried by adding the line e.stopPropagation(); 因此,我尝试通过添加e.stopPropagation();e.stopPropagation();进行尝试e.stopPropagation(); but no success. 但没有成功。

$(".categoryHeading").click(function(e){
            e.stopPropagation();

      $(".items").toggle(1000);

After I set the margin for the ".items" the bounces have gotten disappeared. 在为“ .items”设置边距后,弹跳消失了。 Can anyone explain me why there is a weird behaviour? 谁能解释我为什么会有奇怪的行为?
}); });

JSBIN link JSBIN 链接

The toggle event puts a few lines of style into the <div class="items"> . toggle事件将一些样式行放入<div class="items"> Namely, the following: 即,以下内容:

display: block;
overflow: hidden;
height: 75.9189px; // this value goes up/down
padding: 0px;
margin: 0px;
width: 330.892px;
opacity: 0.89527; // this value goes up/down

After the animation, only one property is still available: display: block; 动画之后,只有一个属性仍然可用: display: block;

If you add overflow: hidden , the bump is gone. 如果您添加了overflow: hidden ,凹凸将消失。 So, add overflow: hidden to .items {} . 因此,添加overflow: hidden.items {}


The problem, however, is caused by the padding , which is part of the <p> element. 但是,问题是由<p>元素的一部分padding引起的。 If the property overflow:hidden; 如果属性overflow:hidden; is set on the parent, the margin around the child ( <p> ) is not used (because the overflow of the parent is hidden). 设置在父级上,则不使用子级( <p> )周围的边距(因为隐藏了父级的溢出)。 This is the reason, it jumps up. 这就是原因,它跳了起来。


Here is how I found out, what properties are set to the element during the JavaScript animation (asked for in the comments): 这是我发现JavaScript动画过程中为元素设置的属性的方式(在注释中要求):

The animation is made with JavaScript. 动画是用JavaScript制作的。 Because it is quite easy to increase the duration of the animation, I've changed this value to 5000. Then, in the Developer Tools (of Chrome) you can go to the Sources tab and stop the execution of JavaScript (Pause icon on the right or just F8 with the focus on the DevTools). 由于增加动画的持续时间非常容易,因此我将该值更改为5000。然后,在Chrome的开发人员工具中,您可以转到“源”标签并停止执行JavaScript(“暂停”图标正确或仅F8(着重于DevTools)。 Afterwards, you can go to the elements tab and see the elements with their styles. 之后,您可以转到元素选项卡并查看元素及其样式。

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

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