Home > Web Front-end > JS Tutorial > body text

jQuery implements the method of sliding left to appear delete button

小云云
Release: 2017-12-29 11:41:46
Original
3619 people have browsed it

This article mainly introduces the example of the delete button appearing on the left slide based on jQuery. The detailed code is compiled here, which is of great practical value. Friends who need it can refer to it. I hope it can help everyone.

Recently when I was working on a project, I encountered a need to implement a delete button effect similar to the QQ conversation list on the left swipe, so I tried to write one and share it with everyone. , God, don’t spray.

Basic requirements

Since we are making a cross-platform APP, part of the interface is actually a webpage loaded by WebView, so we need to use a webpage to achieve this effect: When you slide to the left, the delete button is displayed, and when you slide to the right, the delete button is hidden.

Sample picture of the finished product

Well, let’s take the picture first. The following are the effects in PC browser and Mobile browser respectively.

PC browser


Mobile browser

Implementation ideas

In order to illustrate my implementation ideas, I made two pictures to assist the explanation.

First, please look at Figure 1. In the picture, we set the width of each row to exceed the width of the browser, and the excess part is the area where the button is placed. Since the maximum width of the browser is exceeded, the button area is not visible at this time and only the general information section on the left is displayed.

Figure 1 Normal state

Next, we monitor the general information area on the left and monitor the sliding event (the specific method of monitoring is not considered yet). When we listen to the left swipe event, we offset the corresponding row to the left so that the button is displayed, and the excess left part is blocked (see Figure 2).

Figure 2 Left sliding state

When we slide right, we can return the corresponding row to the time when the left offset is 0 .

Key implementation method

For left sliding and right sliding, we implement it by setting the margin-left of the general information area. When the margin-left is set to negative When the margin-left is set to 0 again, the left slide is realized.

For sliding event monitoring, it is implemented by monitoring the mouse (finger) press and lift, and determine whether to slide right or left based on the positive or negative difference between the X coordinates of the two points.

Complete code

It should be noted that when I tested, I used chrome’s normal mode and mobile simulator mode, and found two modes The next monitor is different, so I wrote two monitors so that at least one of them will be executed. There may be other better adaptation methods, but they are not the focus here. Of course, everyone is welcome to give me advice.

As for the code part, jQuery is used. In fact, there is no problem if it is not used. Both animation sliding and monitoring can be written in pure js, but since this is not the focus here, why not use jQuery? Successful people stand on the shoulders of giants, and we are not as good as jQuery (.・`ω´・)

Updated on 2015/11/13

Yes A classmate pointed out that the code did not have a sliding effect in QQ mobile browser and Opera mobile browser. I looked for the reason and found it probably was the reason mentioned in the post. So based on the tips in the post and the tips of a master classmate of that classmate, I did Made some changes. Mainly in the touchmove event, it is judged whether to block the default event based on the horizontal and vertical coordinate displacements, as follows:


// 横向位移大于纵向位移,阻止纵向滚动
if (Math.abs(delta.x) > Math.abs(delta.y)) {
  event.preventDefault();
}
Copy after login

2016/02/25 update

qq_25558115 classmate mentioned: "If we can provide you with the information that only one record can be swiped left, if you slide other records, the record with left swipe will be returned to the original position." So a simple implementation was carried out. The main ideas are as follows:


// 用一个变量记录上一次左滑的对象
var lastLeftObj;

// 在左滑发生的时候,判定上一个左滑的对象是否存在,若存在,且不是当前被左滑的对象,则将其右滑
// 同时,记录新的左滑对象
// 在右滑发生时,将上一个左滑对象清空
if (左滑) {
  pressedObj左滑
  lastLeftObj && lastLeftObj != pressedObj && lastLeftObj右滑
  lastLeftObj = pressedObj; // 记录上一个左滑的对象
} else if (右滑) {
  pressedObj右滑
  lastLeftObj = null; // 清空上一个左滑的对象
}
Copy after login

Updated on 2016/09/06

Modified according to the bug raised by classmate Ma Canfa:

Make a judgment when swiping right. Only when the object to be swiped right (pressedObj) is the object that was last left swiped (lastLeftObj), slide the object right and clear lastLeftObj.


if (pressedObj == lastLeftObj) {...}
Copy after login

According to girlyougo’s suggestion, add the function of “resetting the current left slide button when clicking in other areas except this row”. The idea is to determine pressedObj!=lastLeftObj at the end of the slide, that is, it is known that the clicked/slided object is another object:


##

// 点击除当前左滑对象之外的任意其他位置
if (lastLeftObj && pressedObj != lastLeftObj) {
  $(lastLeftObj).animate({marginLeft:"0"}, 500); // 右滑
  lastLeftObj = null; // 清空上一个左滑的对象
}
Copy after login

In fact, after adding the above functions, The bug mentioned earlier no longer exists. However, the part of the code that eliminates bugs is retained here.

The updated complete code is as follows:






左划出现删除按钮,右滑隐藏




蜡笔小新

在同行的小伙伴中提到了你

1分钟前

乔巴

你看不到我哦

1分钟前

贱行贱远

回忆里想起模糊的小时候,云朵漂浮在蓝蓝的天空,那时的你说,要和我手牵手,一起走到时间的尽头

1分钟前

小黄人

哈哈哈哈哈……暑假来看小黄人电影哦~哈哈哈……

1分钟前

Copy after login

Summary

The code is still relatively rough, there are many bugs, and some places are not so absolute. For example, when I press it, it is on the first record, and then when I lift it, it is on the second record, then the slide will be on the first record at this time. But this depends on the specific needs. If you think the sliding object should be based on the object when it is pressed, then slide the object when it is pressed no matter where it is lifted; if you think the sliding object should be lifted It's okay if you don't slide any object if you think it's not the same object when you press it and lift it up. In short, it depends on the demand.

Related recommendations:

Based on JS to implement the function of sliding left on the mobile terminal to display the delete button

Example details Angular implements the function of displaying the input content at the top after clicking the button

WeChat applet implements the function of changing the font color by clicking the button

The above is the detailed content of jQuery implements the method of sliding left to appear delete button. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!