【开发小技巧】32—如何利用HTML、CSS和jQueryUI创建拖放功能以对图像进行重新排序?...

本文主要是介绍【开发小技巧】32—如何利用HTML、CSS和jQueryUI创建拖放功能以对图像进行重新排序?...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

来源 | https://www.geeksforgeeks.org/

给定一个图像库,要求通过拖放来重新排列列表或网格中图像的顺序。jQuery UI框架提供了sortable()函数,该函数有助于通过使用鼠标对列表项进行重新排序。使用此功能,列表项可以互换。

jQuery UI提供具有默认可拖动属性的sortable()函数。HTML文档中的所有列表元素都是可以互换的,并可以重新排序以进行显示。

用户可以借助鼠标将元素拖放到新位置。其他元素会自行调整以适合列表。

首先,我们需要创建一个基本的图片库,在其中我们将执行拖放功能以对图片库列表进行重新排序。包括所需的jQuery UI链接和库。

  • 包括所有必需的jQuery和jQuery UI库,代码如下:

<link href =“ https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css” rel =“样式表”>
<script src =“ https:// code。jquery.com/jquery-1.10.2.js”> </ script>
<script src =“ https://code.jquery.com/ui/1.10.4/jquery-ui.js”> </ script>

HTML代码:

<!DOCTYPE html> 
<html> 
<head> <title> How to create drag and drop features for images reorder using HTML CSS and jQueryUI? </title> 
</head> <body> <h1 style="color:green">GeeksforGeeks</h1>  <b>Drag and drop using jQuery UI Sortable</b> <div class="height"></div><br> <div id = "imageListId"> <div id="imageNo1" class = "listitemClass"> <img src="images/geeksimage1.png" alt=""> </div> <div id="imageNo2" class = "listitemClass"> <img src="images/geeksimage2.png" alt=""> </div> <div id="imageNo3" class = "listitemClass"> <img src="images/geeksimage3.png" alt=""> </div> <div id="imageNo4" class = "listitemClass"> <img src="images/geeksimage4.png" alt=""> </div> <div id="imageNo5" class = "listitemClass"> <img src="images/geeksimage5.png" alt=""> </div> <div id="imageNo6" class = "listitemClass"> <img src="images/geeksimage6.png" alt=""> </div> </div> <div id="outputDiv"> <b>Output of ID's of images : </b> <input id="outputvalues" type="text" value="" /> </div> 
</body> </html> 

CSS代码:

<style> /* text align for the body */body { text-align: center; } /* image dimension */img { height: 200px; width: 350px; } /* imagelistId styling */#imageListId { margin: 0; padding: 0; list-style-type: none; } #imageListId div { margin: 0 4px 4px 4px; padding: 0.4em; display: inline-block; } /* Output order styling */#outputvalues { margin: 0 2px 2px 2px; padding: 0.4em; padding-left: 1.5em; width: 250px; border: 2px solid dark-green; background: gray; } .listitemClass { border: 1px solid #006400; width: 350px; } .height { height: 10px; } 
</style>

最后,我们通过添加JavaScript代码来添加拖放功能。

JS代码:

<script> $(function() { $("#imageListId").sortable({ update: function(event, ui) { getIdsOfImages(); } //end update          }); }); function getIdsOfImages() { var values = []; $('.listitemClass').each(function(index) { values.push($(this).attr("id") .replace("imageNo", "")); }); $('#outputvalues').val(values); }  
</script>

最后,我们将整合以上所有的代码,然后,在其中执行拖放操作以对图库中的图像进行重新排序。

  • 最终代码如下:

<!DOCTYPE html>
<html> 
<head> <title> How to create drag and drop features for images reorder using HTML CSS and jQueryUI? </title> <link href =  
"https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"rel = "stylesheet"> <script src =  
"https://code.jquery.com/jquery-1.10.2.js"> 
</script> <script src =  
"https://code.jquery.com/ui/1.10.4/jquery-ui.js"> 
</script> <style> /* text align for the body */ body { text-align: center; } /* image dimension */ img{ height: 200px; width: 350px; } /* imagelistId styling */ #imageListId {  margin: 0;  padding: 0; list-style-type: none; } #imageListId div {  margin: 0 4px 4px 4px; padding: 0.4em;              display: inline-block; } /* Output order styling */ #outputvalues{ margin: 0 2px 2px 2px; padding: 0.4em;  padding-left: 1.5em; width: 250px; border: 2px solid dark-green;  background : gray; } .listitemClass  { border: 1px solid #006400;  width: 350px;      } .height{  height: 10px; } 
</style> <script> $(function() { $( "#imageListId" ).sortable({ update: function(event, ui) { getIdsOfImages(); }//end update          }); }); function getIdsOfImages() { var values = []; $('.listitemClass').each(function (index) { values.push($(this).attr("id") .replace("imageNo", "")); }); $('#outputvalues').val(values); } 
</script> 
</head> <body> <h1 style="color:green">GeeksforGeeks</h1> <b>Drag and drop using jQuery UI Sortable</b> <div class="height"></div><br> <div id = "imageListId"> <div id="imageNo1" class = "listitemClass"> <img src="images/geeksimage1.png" alt=""> </div> <div id="imageNo2" class = "listitemClass"> <img src="images/geeksimage2.png" alt=""> </div> <div id="imageNo3" class = "listitemClass"> <img src="images/geeksimage3.png" alt=""> </div> <div id="imageNo4" class = "listitemClass"> <img src="images/geeksimage4.png" alt=""> </div> <div id="imageNo5" class = "listitemClass"> <img src="images/geeksimage5.png" alt=""> </div> <div id="imageNo6" class = "listitemClass"> <img src="images/geeksimage6.png" alt=""> </div> </div> <div id="outputDiv"> <b>Output of ID's of images : </b> <input id="outputvalues" type="text" value="" /> </div> 
</body> </html> 

最终输出效果:

本文完~

这篇关于【开发小技巧】32—如何利用HTML、CSS和jQueryUI创建拖放功能以对图像进行重新排序?...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/611772

相关文章

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Golang如何用gorm实现分页的功能

《Golang如何用gorm实现分页的功能》:本文主要介绍Golang如何用gorm实现分页的功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录背景go库下载初始化数据【1】建表【2】插入数据【3】查看数据4、代码示例【1】gorm结构体定义【2】分页结构体

python如何创建等差数列

《python如何创建等差数列》:本文主要介绍python如何创建等差数列的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python创建等差数列例题运行代码回车输出结果总结python创建等差数列import numpy as np x=int(in

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)