Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="wrap">
  <div class="column">
    <div class="title todo">待處理</div>
    <div class="input-wrap">
      <input type="text" placeholder="+ 新增事項" />
      <button>新增</button>
    </div>
    <ol class="list"></ol>
  </div>
  <div class="column">
    <div class="title handle">進行中</div>
    <ol class="list"></ol>
  </div>
  <div class="column">
    <div class="title complete">已完成</div>
    <ol class="list"></ol>
  </div>
</div>
<div class="delete">刪除</div>
              
            
!

CSS

              
                body {
  margin: 0;
  background: #f1f1f1;
}
.wrap {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 100px 0 0 0;
}
.column {
  width: 350px;
  min-height: 300px;
  margin: 0 12px;
  border-radius: 12px;
  border: 1px solid #ccc;
  background: white;
  overflow: hidden;
  transition: 0.2s;
}
.title {
  padding: 8px 0;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
  line-height: 26px;
  color: white;
}
.title.todo {
  background: #ff9100;
}
.title.handle {
  background: #00afb8;
}
.title.complete {
  background: #70be20;
}
.list {
  list-style: decimal inside;
  /* list-style: none; */
  padding: 0;
  margin: 0;
}
.item {
  position: relative;
  padding: 6px 12px;
  border-bottom: 1px solid #ddd;
  line-height: 24px;
  transition: 0.2s;
}
.item.dragging {
  background: #e6f9fa;
  opacity: 0.5;
}
.item::before,
.item::after {
  content: "";
  position: absolute;
  display: block;
  width: 100%;
  height: 4px;
  background: lightblue;
  opacity: 0;
}
.item.before::before {
  top: -2px;
  left: 0;
  opacity: 1;
}
.item.after::after {
  bottom: -2px;
  left: 0;
  opacity: 1;
}
.input-wrap {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
}
input {
  flex-grow: 1;
  padding: 6px 12px;
  border: none;
  font-size: 16px;
  line-height: 24px;
  outline: none;
}
button {
  width: 60px;
  padding: 4px;
  margin: 0 8px;
  border-radius: 4px;
  border: none;
  background: #7e7e7e;
  color: white;
  cursor: pointer;
  transition: 0.2s;
}
button:hover {
  background: #a9a9a9;
}
.delete {
  width: 60px;
  height: 60px;
  line-height: 60px;
  font-size: 16px;
  text-align: center;
  border-radius: 50%;
  margin: 30px auto;
  background: #ccc;
  color: #7e7e7e;
}

              
            
!

JS

              
                // 用來暫存被 drag 的元素
let source = null;
function addDragEvt(element) {
  element.addEventListener("dragstart", (e) => {
    e.target.classList.add("dragging");
    source = e.target;
  });
  element.addEventListener("dragend", (e) => {
    e.target.classList.remove("dragging");
    source = null;
  });
}

// 用來暫存被 dragover 的元素
let overItem = null;
// 重置被 dragover 的元素
function clearOverItem() {
  if (!overItem) return;
  overItem.classList.remove("before");
  overItem.classList.remove("after");
  overItem = null;
}

function addDropEvt(element) {
  element.addEventListener("dragover", (e) => {
    clearOverItem();
    // 如果 dragover 的元素也是任務項目且不是目前被 drag 的 source 時執行
    if (e.target.getAttribute("draggable") && e.target !== source) {
      overItem = e.target;

      if (e.offsetY > overItem.offsetHeight / 2) {
        // 如果鼠標在元素的下半部顯示下方的藍條
        overItem.classList.add("after");
      } else {
        // 反之,顯示上方的藍條
        overItem.classList.add("before");
      }
    }
    e.preventDefault();
  });
  element.addEventListener("drop", (e) => {
     const list = e.currentTarget.querySelector("ol");
    if (overItem) {
      if (overItem.classList.contains("before")) {
        // 如果 overItem 有 before class 就將 source 移動到它的前面
        list.insertBefore(source, overItem);
      } else {
        // 反之,有 after class 就將 source 移動到它的後面
        list.insertBefore(source, overItem.nextElementSibling);
      }
    } else {
      // 如果沒有 overItem 也沒有更換狀態就不動作
      if (e.currentTarget.contains(source)) return;
      // 反之,加到最後面
      else list.appendChild(source);
    }
    clearOverItem();
  });
}

const input = document.querySelector("input");
const button = document.querySelector("button");
const todoList = input.parentElement.nextElementSibling;
function createToDo(content) {
  const newItem = document.createElement("li");
  newItem.classList.add("item");
  // 記得要加上 draggable,這樣任務才可以拖曳
  newItem.setAttribute("draggable", true);
  newItem.textContent = content;
  // 記得在 createToDo 中加入這一行來為新增的 li 監聽事件
  addDragEvt(newItem);
  todoList.appendChild(newItem);
  input.value = "";
}

input.addEventListener("keydown", (e) => {
  if (!input.value.trim() || e.which !== 13) return;
  createToDo(input.value);
});

button.addEventListener("click", () => {
  if (!input.value.trim()) return;
  createToDo(input.value);
});

const columns = document.querySelectorAll(".column");
columns.forEach((column) => {
  addDropEvt(column);
});

const del = document.querySelector(".delete");
del.addEventListener("dragover", (e) => {
  e.preventDefault();
});
del.addEventListener("drop", (e) => {
  source.remove();
  clearOverItem();
});
              
            
!
999px

Console