当前位置: 首页 > news >正文

SortableJS/Sortable拖拽组件,使用详细(Sortablejs安装使用)

简述

作为一名前端开发人员,在工作中难免会遇到拖拽功能,分享一个github上一个不错的拖拽js库,能满足我们在项目开发中的需要,支持VueReact,下面是SortableJS的使用详细;

 

这个是sortableJS中文官方文档,记录了一些相关属性和使用方法,页面十分简约方便阅读;

SortableJs中文文档http://www.sortablejs.com

下面是使用详细,主要分为3步;

1、首相需要安装相关依赖:npm install sortablejs --save;

2、页面引入使用:import Sortable from "sortablejs";

3、获取大盒子属性,然后在初始化的使用需要调用Sortable.create()方法;

 

vue页面使用;

<template><div class="sortable"><div id="container" ref="sortableBox"><div v-for="(item, index) in arr" :key="item"><span>{{ item }}</span></div></div></div>
</template><script>
import Sortable from "sortablejs";
export default {name: "CodeDemoSortable",data() {return {arr: ["one", "two", "three", "four"],};},mounted() {this.defineSortable();},methods: {defineSortable() {this.$nextTick(() => {const el = this.$refs.sortableBox;// 或则使用// const el = document.getElementById("container");Sortable.create(el, {animation: 200,//拖拽动画过渡时间group: "name",sort: true,});});},},
};
</script><style lang="scss" scoped>
.sortable {#container {width: 600px;height: 300px;margin: 50px auto;background-color: skyblue;padding: 20px;box-sizing: border-box;display: flex;flex-direction: column;justify-content: space-around;div {height: 20%;text-align: center;line-height: 50px;width: 100%;background-color: plum;}}
}
</style>

 

html页面使用;

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.container {width: 600px;height: 300px;margin: 50px auto;background-color: skyblue;padding: 20px;box-sizing: border-box;display: flex;flex-direction: column;justify-content: space-around;}.container>div {height: 20%;text-align: center;line-height: 52px;width: 100%;background-color: plum;}</style>
</head><body><div class="container"><div>one</div><div>two</div><div>three</div></div><!-- 引入js文件,也可以把js文件下载到本地使用 --><script src="https://cdn.bootcdn.net/ajax/libs/Sortable/1.15.0/Sortable.min.js"></script><script>const el = document.querySelector('.container')// 初始化var sortable = Sortable.create(el, {animation: 200,//拖拽动画过渡时间group: "name",sort: true});</script>
</body></html>

 

效果图如下:

2fff1f879b4e40cba11b027ff70a8807.gif

 

感觉有用,就一键三连,感谢(●'◡'●)

 

http://www.lryc.cn/news/13768.html

相关文章:

  • Heartbeat+Nginx实验
  • JavaEE|网络编程之套接字 TCP
  • Robot Framework自动化测试---元素定位
  • ASP.NET Core中的路由
  • VBA提高篇_26 Textbox多行_ListBox_ComboBox
  • python环境配置
  • 集算器连接外部库
  • 力扣刷题|216.组合总和 III、17.电话号码的字母组合
  • 机器学习笔记之谱聚类(一)k-Means聚类算法介绍
  • 云原生周刊 | 2023 年热门:云 IDE、Web Assembly 和 SBOM | 2023-02-20
  • python 打包EXE
  • CANopen概念总结、心得体会
  • 【2】MYSQL数据的导入与导出
  • Kaggle系列之CIFAR-10图像识别分类(残差网络模型ResNet-18)
  • ESP-C3入门11. 创建最基本的HTTP请求
  • K8S+Jenkins+Harbor+Docker+gitlab集群部署
  • 看见统计——第四章 统计推断:频率学派
  • 2023年2月访问学者博士后热门国家出入境政策变化汇总
  • “离开浪浪山”是假象,80%年轻人下班后还在学习,真实是想先上个山。
  • Kotlin 33. CompileSdkVersion 和 targetSdkVersion 有什么区别?
  • 实用调试技巧——“C”
  • JavaScript - 函数
  • Cesium 卫星轨迹、卫星通信、卫星过境,模拟数据传输。
  • 2023年湖北中级职称(工程类建筑类)报名条件和要求是什么?
  • socket编程复习
  • 深度学习神经网络基础知识(三)前向传播,反向传播和计算图
  • 一图说明 monorepo 落地流程方案
  • SAP ABAP WRITE语法大全
  • 微信小程序自定义全局组件showModal
  • 4|无线传感器网络与应用|无线传感器网络原理及方法-许毅版|考试知识点