首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用.slideToggle()时的jQuery跳转效果

问使用.slideToggle()时的jQuery跳转效果
EN

Stack Overflow用户
提问于 2020-04-03 19:20:55
回答 1查看 50关注 0票数 0

我参加了一个Udemy Bootcamp,其中包括使用jQuery编写待办事项列表。该程序要求文本输入在按下“最小化”按钮时消失,但一旦输入消失,所有较低的元素就会“跳起来”。我想我可以用幻灯片效果代替它来改进它。

问题是输入向上滑动,然后几乎消失,然后突然“跳”起来。我已经在stackOverflow上看过了,它似乎与大小/边距或容器有关,但通过摆弄它(甚至通过控制台设置和/或删除它),我仍然无法消除这种影响,我想我需要一些帮助。

​

代码语言:javascript
复制
// Check off todos while clicking. Use on(click) to add event listener 
// to future elements not yet on page when the code first runs. We apply it to the whole ul for
// ("click", "li") means the event listener is applied to the li inside the ul
$("ul").on("click", "li", function() {
  $(this).toggleClass("completed");
});

// Click on X to delete Todo
$("ul").on("click", "span", function(event) {
  $(this).parent().fadeOut(500, function() {
    $(this).remove();
  });
  event.stopPropagation();
});

$("input[type='text']").keypress(function(event) {
  if (event.which === 13) {
    // grab the text of the new todo from the input
    var todoText = $(this).val();
    $(this).val("");
    // create new li to add to the ul
    $("ul").append("<li>" + "<span><i class='fa fa-trash'></i></span> " + todoText + "</li>");
  }
})

// Make the plus sign collapse the text input
$(".fa-plus").click(function() {
  $("input").slideToggle(function() {

  });
});
代码语言:javascript
复制
body {
  background: #2980B9;
  /* fallback for old browsers */
  background: -webkit-linear-gradient(to right, #FFFFFF, #6DD5FA, #2980B9);
  /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to right, #FFFFFF, #6DD5FA, #2980B9);
  /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}

#container {
  width: 360px;
  margin: 5% auto;
  background: #f7f7f7;
  box-shadow: 0 0 3px rgba(0, 0, 0, 0.1);
}

.completed {
  color: gray;
  text-decoration: line-through;
}

.fa-plus {
  float: right;
}

h1 {
  background: #2980b9;
  color: white;
  margin: 0;
  padding: 10px 20px;
  text-transform: uppercase;
  font-size: 24px;
  font-weight: normal;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  background: white;
  height: 40px;
  line-height: 2.5em;
}

li:nth-child(2n) {
  background: #f7f7f7;
}

input {
  font-size: 18px;
  background-color: #f7f7f7;
  width: 360px;
  padding: 13px 13px 13px 20px;
  box-sizing: border-box;
  border: 3px solid rgba(0, 0, 0, 0);
}

input:focus {
  background: white;
  border: 3px solid #2980b9;
  outline: none;
}

span {
  background: red;
  height: 40px;
  margin-right: 40px;
  text-align: center;
  color: white;
  width: 0;
  display: inline-block;
  opacity: 0;
  transition: 0.2s linear;
}

li:hover span {
  width: 40px;
  opacity: 1;
}

.hidden {
  display: none;
}
代码语言:javascript
复制
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<div id="container">
  <h1>To-Do List<i class="fa fa-plus"></i></h1>
  <input type="text" placeholder="Add new Todo">
  <ul>
    <li><span><i class="fa fa-trash"></i></span> First item</li>
    <li><span><i class="fa fa-trash"></i></span> Second item</li>
    <li><span><i class="fa fa-trash"></i></span> Third item</li>
  </ul>
</div>

​

这只是一个片段。我将整个代码上传到了Fiddle上,如果有任何帮助,我将不胜感激

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-03 19:56:03

使用div包装输入,而不是直接切换input。然后切换div

要进行的更改:

代码语言:javascript
复制
<div id='input'>
   <input type="text" placeholder="Add new Todo">
</div>
代码语言:javascript
复制
$(".fa-plus").click(function() {
    $("#input").slideToggle();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61010738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档