In the development of modern web applications, interaction design is often a crucial part. One of the determinants of good user experience is the appropriate form of interaction.
Among them, Swipe-to-Delete is widely used to delete elements such as list items or cards. For example, deleting an email in the mail application, or deleting an update in the social network application, etc.
In traditional web development, implementing left-swipe deletion usually requires a lot of manual processing of DOM operations and style control. But now, the event and animation libraries provided by jQuery have greatly simplified and enriched the way to implement left swipe to delete.
Next, we will use jQuery to implement a simple left-swipe delete effect.
- Realize basic interaction
First, in the HTML page, we need to define a container element containing the list items and add a block element containing the delete button to it. The delete icon provided by the FontAwesome font icon library is used here, and some basic styles need to be added to the delete button:
<div class="list-item"> <span>这是一个列表项</span> <div class="delete"> <i class="fa fa-trash"></i> </div> </div> <style> .list-item { height: 50px; line-height: 50px; background-color: #fff; overflow: hidden; position: relative; } .delete { width: 50px; height: 50px; position: absolute; top: 0; right: -50px; background-color: #f00; color: #fff; text-align: center; cursor: pointer; transition: all 0.3s ease; } </style>
The above code defines a list item container with a height of 50 pixels, which contains a span element (used to display the list item content) and a delete button element. The container element has the overflow:hidden attribute set to hide the delete button element. The delete button element is set with absolute positioning and a negative offset on the right side to achieve the animation effect when sliding left to delete.
Next, add jQuery mouse touch event monitoring for the delete button element to handle the logic of left-swipe deletion. Due to the implementation of left-swipe deletion, essentially the movement distance of the mouse causes the position of the delete button to change, so the mouse movement event needs to be monitored.
$(document).on("mousemove swipeleft",".list-item",function(e){ var $delete = $(this).find('.delete'); var left = e.pageX - $(this).offset().left; if (left > $(this).width()/2) { $delete.css('right', 0); } else { $delete.css('right', '-50px'); } });
In the above code, we use jQuery's on() method to listen for mouse movement and touch events (left sliding events). Then get the distance of the mouse relative to the left side of the element, and determine the position of the delete button based on half the width of the element: if the mouse movement distance exceeds half the width of the element, the right offset animation of the delete button element transitions to 0, otherwise the right The offset animation transitions to -50px to slide the delete button into the element.
- Add deletion operation
After processing the interaction of left swipe to delete, you also need to consider how to implement the logic of the deletion operation. A simple way to achieve this is to use jQuery's remove() method to delete elements. The implementation here is to add a click event listener after the right slide to delete is triggered, in which the element is removed from the DOM tree by calling the remove() method.
$(document).on("click",".delete",function(e){ e.stopPropagation(); $(this).parent().remove(); });
In the above code, we use jQuery's stopPropagation() method to prevent click events from bubbling up to the parent element and affecting left-swipe deletion monitoring.
- Complete code
Finally, by putting the above two parts of code together, we can get a complete jQuery left slide delete implementation code.
<div class="list-item"> <span>这是一个列表项</span> <div class="delete"> <i class="fa fa-trash"></i> </div> </div> <style> .list-item { height: 50px; line-height: 50px; background-color: #fff; overflow: hidden; position: relative; } .delete { width: 50px; height: 50px; position: absolute; top: 0; right: -50px; background-color: #f00; color: #fff; text-align: center; cursor: pointer; transition: all 0.3s ease; } </style> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).on("mousemove swipeleft",".list-item",function(e){ var $delete = $(this).find('.delete'); var left = e.pageX - $(this).offset().left; if (left > $(this).width()/2) { $delete.css('right', 0); } else { $delete.css('right', '-50px'); } }); $(document).on("click",".delete",function(e){ e.stopPropagation(); $(this).parent().remove(); }); </script>
This code is just a simple example of jQuery left-swipe deletion. It does not include complete detailed processing and suitability optimization. Readers can make appropriate modifications and extensions according to their own needs.
The above is the detailed content of jquery left slide to delete. For more information, please follow other related articles on the PHP Chinese website!

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
