RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 621163
Accepted
Игорь
Игорь
Asked:2020-01-30 17:45:33 +0000 UTC2020-01-30 17:45:33 +0000 UTC 2020-01-30 17:45:33 +0000 UTC

TagBar的逻辑怎么写?

  • 772

你好!有以下构造:

  • popup-window带有类别列表的模态窗口(单击 时出现add-category);
  • categories-list- 放置所选类别的块。

逻辑如下:一个人按下按钮Add Category,出现一个模态窗口 - 在这里选择必要的类别,按下它Choose并且标有所选类别的类别出现在块字段中categories-list。每个块类别categories-list都有一个“删除”按钮 - 即。通过单击它,类别消失并:checked在模态中被删除。

一个很大的要求来解释如何做到这一点,因为jQuery我有一个级别addClass/ removeClass。

沙盒

$('.categories-list .add-category').click(function () {
  if ($('#select-category-popup').css('display') == 'none') {
    $('#select-category-popup').fadeIn();
  }
});
$('.close-modal-window').click(function () {
  $('#select-category-popup').fadeOut();
});
$('.choose-btn').click(function() {
  $('#select-category-popup').fadeOut();
})
.categories-list {
  padding: 10px 20px 0 20px;
  border-bottom: none;
  list-style: none;
  font-size: 0;
  line-height: 0;
  border: 1px solid #333;
}
.categories-list .category {
  display: inline-block;
  margin: 0 10px 10px 0;
  position: relative;
  border: 1px solid transparent;
  padding: 6px 32px 4px 10px;
  font-size: 14px;
  line-height: 14px;
  border-radius: 4px;
  cursor: pointer;
  -moz-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
  -o-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
  -webkit-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
  transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
}
.categories-list .category.selected-category {
    background: #eeeeee;
}
.categories-list .category .category-icon {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 32px;
}
.categories-list .category .category-icon:after {
    content: '';
    font-family: 'FontAwesome';
    font-size: 16px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    -moz-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
    -o-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
    -webkit-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
    transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
}
.categories-list .category .category-icon.delete:after {
    content: '\f00d';
    padding-bottom: 1px;
    color: #aaa;
}
.categories-list .category .category-icon.add:after {
    content: '\f067';
    padding-top: 1px;
    color: #000;
}
.categories-list .category.add-category {
    background: #ffe69a;
}
.categories-list .category:hover {
    box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);
}
.categories-list .category.add-category:hover {
    border: 1px solid #fbbc25;
}
.categories-list .category.selected-category:hover {
    border: 1px solid #aaa;
    background: #eeeeee;
}
.categories-list .category.selected-category:hover:after {
}
.categories-list .category .category-icon.delete:hover:after {
    color: #333;
}


.category-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
#select-category-popup {
  position: relative;
  display: none;
  width: 200px;
  margin: 30px auto;
  box-shadow: 0 0 10px 0 rgba(0,0,0,.5);
  padding: 20px;
}
#select-category-popup .close-modal-window {
  position: absolute;
  top: 20px;
  right: 20px;
  cursor: pointer;
}


.choose-btn {
  width: 100%;
  margin-top: 20px;
  cursor: pointer;
  display: inline-block;
  text-align: center;
  color: red;
}
<script src="https://use.fontawesome.com/ea0959dcb8.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="categories-list">
  <li class="category selected-category">
    Action
    <span class="category-icon delete"></span>
  </li>
  <li class="category add-category">
    Add category
    <span class="category-icon add"></span>
  </li>
</ul>

<div id="select-category-popup" class="popup-window">
  <a class="close-modal-window">
    <i class="fa fa-times" aria-hidden="true"></i>
  </a>
  <div class="select-block--container">
    <ul class="category-list">
      <li class="category-item">
        <input id="category-action" type="checkbox">
        <label for="category-action">Action</label>
      </li>
      <li class="category-item">
        <input id="category-adventure" type="checkbox">
        <label for="category-adventure">Adventure</label>
      </li>
      <li class="category-item">
        <input id="category-card" type="checkbox">
        <label for="category-card">Card</label>
      </li>
    </ul>
    
    <span class="choose-btn">Choose</span>
  </div>
</div>

javascript
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    user220409
    2020-01-30T23:33:08Z2020-01-30T23:33:08Z

    在您的代码上,尤其是在干净js且尽可能清晰的代码上-

    const tagList = document.body.querySelector('.categories-list');
    
    const showSelectionTagFormButton = tagList.querySelector('.add-category');
    showSelectionTagFormButton.addEventListener('click', showSelectionTagFormButton_clickHandler);
    
    const createTagButton = document.body.querySelector('.choose-btn');
    createTagButton.addEventListener('click', createTagButton_clickHandler);
    
    function render(template){
    	let element = document.createElement('div');
    
    	element.innerHTML = template;
    
    	let result = element.removeChild(element.firstChild);
    
    	return result;
    }
    
    const Template = {
    	tag({title}){
    		return (`
    			<li class="category selected-category">
    				${ title }
    				<span class="category-icon delete"></span>
    				</li>
    		`).trim();
    	}
    }
    function create(data){
    	let template = Template.tag(data);
    	let tag = render(template);
    
    	initialization(tag);
    
    	tagList.insertBefore(tag, tagList.firstChild);
    
    	return tag;
    }
    function destroy(tag){
    	uninitialization(tag);
    
    	tagList.removeChild(tag);
    }
    
    function initialization(tag){
    	tag.addEventListener('click', tag_clickHandler);
    }
    function uninitialization(tag){
    	tag.removeEventListener('click', tag_clickHandler);
    }
    
    function fill( data ){
    	for(let currentData of data){
    		let tag = create( currentData );
    	}
    }
    function clear(){
    	let tags = tagList.querySelectorAll('.selected-category');
    
    	for(tag of tags){
    		destroy(tag);
    	}
    }
    
    function getCheckboxData(checkboxs){
    	let data = [];
    
    	for(let checkbox of checkboxs){
    		data.push({
    			title: checkbox.dataset.title
    		})
    	}
    
    	return data;
    }
    
    function showSelectionTagForm(){
    	document.body.querySelector('#select-category-popup').style.display = "block";
    	document.body.querySelector('.fa-times').addEventListener('click',hideSelectionTagFormButton_clickHandler);
    }
    function hideSelectionTagForm(){
    	document.body.querySelector('#select-category-popup').style.display = "none";
    	document.body.querySelector('.fa-times').removeEventListener('click',hideSelectionTagFormButton_clickHandler);
    }
    
    
    function showSelectionTagFormButton_clickHandler(event){
    	showSelectionTagForm();
    }
    
    function hideSelectionTagFormButton_clickHandler(event){
    	hideSelectionTagForm();
    }
    
    function createTagButton_clickHandler(event){
    	let categoryList = document.body.querySelector('.category-list');
    	let checkboxs = categoryList.querySelectorAll('input[type="checkbox"]:checked');
    
    	let data = getCheckboxData(checkboxs)
    		.sort( ( a, b ) => a.title - b.title )
    		.reverse();
    
    	clear();
    	fill(data);
    
    	hideSelectionTagForm();
    }
    
    function tag_clickHandler({target, currentTarget}){
    	if(target.classList.contains('delete')){
    		destroy(currentTarget);
    	}
    }
    .categories-list {
      padding: 10px 20px 0 20px;
      border-bottom: none;
      list-style: none;
      font-size: 0;
      line-height: 0;
      border: 1px solid #333;
    }
    .categories-list .category {
      display: inline-block;
      margin: 0 10px 10px 0;
      position: relative;
      border: 1px solid transparent;
      padding: 6px 32px 4px 10px;
      font-size: 14px;
      line-height: 14px;
      border-radius: 4px;
      cursor: pointer;
      -moz-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
      -o-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
      -webkit-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
      transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
    }
    .categories-list .category.selected-category {
      background: #eeeeee;
    }
    .categories-list .category .category-icon {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      width: 32px;
    }
    .categories-list .category .category-icon:after {
      content: '';
      font-family: 'FontAwesome';
      font-size: 16px;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      -moz-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
      -o-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
      -webkit-transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
      transition: all 0.4s cubic-bezier(0.2, 0.57, 0.36, 0.8);
    }
    .categories-list .category .category-icon.delete:after {
      content: '\f00d';
      padding-bottom: 1px;
      color: #aaa;
    }
    .categories-list .category .category-icon.add:after {
      content: '\f067';
      padding-top: 1px;
      color: #000;
    }
    .categories-list .category.add-category {
      background: #ffe69a;
    }
    .categories-list .category:hover {
      box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1);
    }
    .categories-list .category.add-category:hover {
      border: 1px solid #fbbc25;
    }
    .categories-list .category.selected-category:hover {
      border: 1px solid #aaa;
      background: #eeeeee;
    }
    .categories-list .category.selected-category:hover:after {} .categories-list .category .category-icon.delete:hover:after {
      color: #333;
    }
    .category-list {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    #select-category-popup {
      position: relative;
      display: none;
      width: 200px;
      margin: 30px auto;
      box-shadow: 0 0 10px 0 rgba(0, 0, 0, .5);
      padding: 20px;
    }
    #select-category-popup .close-modal-window {
      position: absolute;
      top: 20px;
      right: 20px;
      cursor: pointer;
    }
    .choose-btn {
      width: 100%;
      margin-top: 20px;
      cursor: pointer;
      display: inline-block;
      text-align: center;
      color: red;
    }
    <script src="https://use.fontawesome.com/ea0959dcb8.js"></script>
    <ul class="categories-list">
      <li class="category add-category">
        Add category
        <span class="category-icon add"></span>
      </li>
    </ul>
    
    <div id="select-category-popup" class="popup-window">
      <a class="close-modal-window">
        <i class="fa fa-times" aria-hidden="true"></i>
      </a>
      <div class="select-block--container">
        <ul class="category-list">
          <li class="category-item">
            <input id="category-action" type="checkbox" data-title="Action">
            <label for="category-action">Action</label>
          </li>
          <li class="category-item">
            <input id="category-adventure" type="checkbox" data-title="Adventure">
            <label for="category-adventure">Adventure</label>
          </li>
          <li class="category-item">
            <input id="category-card" type="checkbox" data-title="Card">
            <label for="category-card">Card</label>
          </li>
        </ul>
    
        <span class="choose-btn">Choose</span>
      </div>
    </div>

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5