With the continuous development of Internet technology, the demand for interactivity of website pages is getting higher and higher. Horizontal dragging and deletion is one of the more common interaction scenarios. When implementing this function, jQuery is a very useful tool that can help us quickly implement this function. This article will introduce in detail how to use jQuery to implement the horizontal drag and delete function.
1. Functional requirements
Before the specific implementation, let’s first take a look at the functional requirements:
- When the mouse clicks on an element, the element changes to is draggable.
- As the mouse is dragged horizontally, the element moves with the mouse.
- When the distance between the element and the right edge of the page reaches a certain value, the element automatically disappears.
- During the dragging process, you can delete the element by releasing the mouse or dragging the element to the specified area.
2. Implementation steps
With the functional requirements in mind, the specific implementation can begin. The specific steps are as follows:
1. Introduce the jQuery library
Before implementing this function, you first need to introduce the jQuery library into the HTML page. In this article, we will introduce jQuery's CDN link, the code is as follows:
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
2. Add HTML elements
Add elements that need to be dragged and deleted in the HTML page, the code is as follows:
<div class="item">元素1</div> <div class="item">元素2</div> <div class="item">元素3</div> <div class="item">元素4</div> <div class="item">元素5</div>
Among them, .item
is the class name we added to each element, which will be used later.
3. Add CSS style
Set the corresponding style for each element. The code is as follows:
.item { width: 100px; height: 100px; background-color: #ccc; border-radius: 5px; text-align: center; margin-right: 10px; float: left; cursor: move; } .clearfix::after { content: ""; display: block; clear: both; }
Among them, the width, height, background color, and circle of the element are set. Corner, text centering, right margin, floating and mouse style and other related properties, and a clearfix
class is added for clearing floating.
4. Add JS interaction code
Add the corresponding JS code to realize the drag and delete function. The code is as follows:
$(function () { var startX = 0; var endX = 0; var offsetX = 0; var isMoved = false; // 当鼠标按下时 $('.item').on('mousedown', function (e) { startX = e.clientX; $(this).css('cursor', 'grabbing'); isMoved = false; }); // 当鼠标移动时 $(document).on('mousemove', function (e) { if (startX === 0) { return; } endX = e.clientX; offsetX = endX - startX; $('.item').css('transform', 'translateX(' + offsetX + 'px)'); if (Math.abs(offsetX) > 10) { isMoved = true; } }); // 当鼠标抬起时 $(document).on('mouseup', function () { $('.item').css('transform', 'translateX(0px)'); $('.item').css('cursor', 'move'); if (isMoved) { if (offsetX > 100) { $(this).remove(); } } startX = 0; endX = 0; offsetX = 0; isMoved = false; }); });
The above code realizes that when the mouse is pressed, Add the corresponding transform
attribute to the element so that the element moves with the mouse, and determine whether the element needs to be deleted during the dragging process.
3. Notes
When implementing the horizontal drag and delete function, you need to pay attention to the following points:
1. Add the corresponding cursor for each element
Attributes enable the corresponding mouse style to be displayed when the mouse is on the element.
2. When using the transform
attribute, you need to use a specific browser kernel to be compatible with different browsers.
3. It is necessary to perform anti-shake processing on mouse movement events to avoid frequent redraws that cause the browser to freeze.
4. Summary
Through the above steps, we successfully implemented the horizontal drag and delete function using jQuery. In actual development, you can also add animation effects to improve the user interaction experience. I hope this article will be helpful to you, and I also hope that you can flexibly use jQuery and its related technologies in future actual development to achieve richer interactive effects.
The above is the detailed content of jquery horizontal drag to delete. For more information, please follow other related articles on the PHP Chinese website!

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
