Home Web Front-end JS Tutorial js imitates the operation of sliding the contact to the left and sliding out the delete button in QQ

js imitates the operation of sliding the contact to the left and sliding out the delete button in QQ

Jun 25, 2018 pm 04:49 PM

This article mainly introduces the operation of sliding the contact to the left and sliding out the delete button in js imitating QQ, that is, writing a js plug-in for sliding left to delete interactive effects. Interested friends can refer to it

The example in this article is to share with you the operation similar to that of contacts in QQ: slide to the left and slide out the delete button. If you release it when it slides more than halfway, it will automatically slide to the bottom. If you release it when it is less than halfway, it will return to the original position.

Pure js implementation
Uses h5's touchmove and other events, and uses js to dynamically change the translate attribute of css3 to achieve animation effects:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1">
 <title>html5向左滑动删除特效</title>
 <style>
 * {
  padding: 0;
  margin: 0;
  list-style: none;
 }
 
 header {
  background: #f7483b;
  border-bottom: 1px solid #ccc
 }
 
 header h2 {
  text-align: center;
  line-height: 54px;
  font-size: 16px;
  color: #fff
 }
 
 .list-ul {
  overflow: hidden
 }
 
 .list-li {
  line-height: 60px;
  border-bottom: 1px solid #fcfcfc;
  position: relative;
  padding: 0 12px;
  color: #666;
  background: #f2f2f2;
  -webkit-transform: translateX(0px);
 }
 
 .btn {
  position: absolute;
  top: 0;
  right: -80px;
  text-align: center;
  background: #ffcb20;
  color: #fff;
  width: 80px
 }
 </style>
 <script>
 /*
  * 描述:html5苹果手机向左滑动删除特效
  */
 window.addEventListener(&#39;load&#39;, function() {
  var initX; //触摸位置
  var moveX; //滑动时的位置
  var X = 0; //移动距离
  var objX = 0; //目标对象位置
  window.addEventListener(&#39;touchstart&#39;, function(event) {
   event.preventDefault();
   var obj = event.target.parentNode;
   if (obj.className == "list-li") {
    initX = event.targetTouches[0].pageX;
    objX = (obj.style.WebkitTransform.replace(/translateX\(/g, "").replace(/px\)/g, "")) * 1;
   }
   if (objX == 0) {
    window.addEventListener(&#39;touchmove&#39;, function(event) {
     event.preventDefault();
     var obj = event.target.parentNode;
     if (obj.className == "list-li") {
      moveX = event.targetTouches[0].pageX;
      X = moveX - initX;
      if (X >= 0) {
       obj.style.WebkitTransform = "translateX(" + 0 + "px)";
      } else if (X < 0) {
       var l = Math.abs(X);
       obj.style.WebkitTransform = "translateX(" + -l + "px)";
       if (l > 80) {
        l = 80;
        obj.style.WebkitTransform = "translateX(" + -l + "px)";
       }
      }
     }
    });
   } else if (objX < 0) {
    window.addEventListener(&#39;touchmove&#39;, function(event) {
     event.preventDefault();
     var obj = event.target.parentNode;
     if (obj.className == "list-li") {
      moveX = event.targetTouches[0].pageX;
      X = moveX - initX;
      if (X >= 0) {
       var r = -80 + Math.abs(X);
       obj.style.WebkitTransform = "translateX(" + r + "px)";
       if (r > 0) {
        r = 0;
        obj.style.WebkitTransform = "translateX(" + r + "px)";
       }
      } else { //向左滑动
       obj.style.WebkitTransform = "translateX(" + -80 + "px)";
      }
     }
    });
   }

  })
  window.addEventListener(&#39;touchend&#39;, function(event) {
   event.preventDefault();
   var obj = event.target.parentNode;
   if (obj.className == "list-li") {
    objX = (obj.style.WebkitTransform.replace(/translateX\(/g, "").replace(/px\)/g, "")) * 1;
    if (objX > -40) {
     obj.style.WebkitTransform = "translateX(" + 0 + "px)";
     objX = 0;
    } else {
     obj.style.WebkitTransform = "translateX(" + -80 + "px)";
     objX = -80;
    }
   }
  })
 })
 </script>
</head>

<body>
 <header>
  <h2>消息列表</h2>
 </header>
 <section class="list">
  <ul class="list-ul">
   <li id="li" class="list-li">
    <p class="con">
     你的快递到了,请到楼下签收
    </p>
    <p class="btn">删除</p>
   </li>
   <li class="list-li">
    <p class="con">
     哇,你在干嘛,快点来啊就等你了
    </p>
    <p class="btn">删除</p>
   </li>
  </ul>
 </section>
</body>

</html>
Copy after login

Made into zepto plug-in
In actual projects, we may use this function in many places. Now we will make this function into a zepto plug-in for easy use later.

For this plug-in, we only implement this function, and then pass in the parameters (the style name of the delete button) to let the program calculate the required sliding distance in js for easy reuse.

zepto.touchWipe.js

/**
 * zepto插件:向左滑动删除动效
 * 使用方法:$(&#39;.itemWipe&#39;).touchWipe({itemDelete: &#39;.item-delete&#39;});
 * 参数:itemDelete 删除按钮的样式名
 */
;
(function($) {
 $.fn.touchWipe = function(option) {
  var defaults = {
   itemDelete: &#39;.item-delete&#39;, //删除元素
  };
  var opts = $.extend({}, defaults, option); //配置选项

  var delWidth = $(opts.itemDelete).width();

  var initX; //触摸位置
  var moveX; //滑动时的位置
  var X = 0; //移动距离
  var objX = 0; //目标对象位置
  $(this).on(&#39;touchstart&#39;, function(event) {
   event.preventDefault();
   var obj = this;
   initX = event.targetTouches[0].pageX;
   objX = (obj.style.WebkitTransform.replace(/translateX\(/g, "").replace(/px\)/g, "")) * 1;
   if (objX == 0) {
    $(this).on(&#39;touchmove&#39;, function(event) {
     event.preventDefault();
     var obj = this;
     moveX = event.targetTouches[0].pageX;
     X = moveX - initX;
     if (X >= 0) {
      obj.style.WebkitTransform = "translateX(" + 0 + "px)";
     } else if (X < 0) {
      var l = Math.abs(X);
      obj.style.WebkitTransform = "translateX(" + -l + "px)";
      if (l > delWidth) {
       l = delWidth;
       obj.style.WebkitTransform = "translateX(" + -l + "px)";
      }
     }
    });
   } else if (objX < 0) {
    $(this).on(&#39;touchmove&#39;, function(event) {
     event.preventDefault();
     var obj = this;
     moveX = event.targetTouches[0].pageX;
     X = moveX - initX;
     if (X >= 0) {
      var r = -delWidth + Math.abs(X);
      obj.style.WebkitTransform = "translateX(" + r + "px)";
      if (r > 0) {
       r = 0;
       obj.style.WebkitTransform = "translateX(" + r + "px)";
      }
     } else { //向左滑动
      obj.style.WebkitTransform = "translateX(" + -delWidth + "px)";
     }
    });
   }

  })
  $(this).on(&#39;touchend&#39;, function(event) {
   event.preventDefault();
   var obj = this;
   objX = (obj.style.WebkitTransform.replace(/translateX\(/g, "").replace(/px\)/g, "")) * 1;
   if (objX > -delWidth / 2) {
    obj.style.transition = "all 0.2s";
    obj.style.WebkitTransform = "translateX(" + 0 + "px)";
    obj.style.transition = "all 0";
    objX = 0;
   } else {
    obj.style.transition = "all 0.2s";
    obj.style.WebkitTransform = "translateX(" + -delWidth + "px)";
    obj.style.transition = "all 0";
    objX = -delWidth;
   }
  })

  //链式返回
  return this;
 };

})(Zepto);
Copy after login

touchWipe.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" id="viewport" content="width=device-width, initial-scale=1">
<title>html5向左滑动删除特效</title>

<style>
  *{ padding:0; margin:0; list-style: none;}
  header{ background: #f7483b; border-bottom: 1px solid #ccc}
  header h2{ text-align: center; line-height: 54px; font-size: 16px; color: #fff}
  .list-ul{ overflow: hidden}
  .list-li{ line-height: 60px; border-bottom: 1px solid #fcfcfc; position:relative;padding: 0 12px; color: #666;
    background: #f2f2f2;
    -webkit-transform: translateX(0px);
  }
  .btn{ position: absolute; top: 0; right: -80px; text-align: center; background: #ffcb20; color: #fff; width: 80px}
</style>

</head>
<body>
<header>
  <h2>消息列表</h2>
</header>
<section class="list">
  <ul class="list-ul">
    <li id="li" class="list-li">
      <p class="con">
        你的快递到了,请到楼下签收
      </p>
      <p class="btn">删除</p>
    </li>
    <li class="list-li">
      <p class="con">
        哇,你在干嘛,快点来啊就等你了
      </p>
      <p class="btn">删除</p>
    </li>
  </ul>
</section>

<p>X: <span id="X"></span></p>
<p>objX: <span id="objX"></span></p>
<p>initX: <span id="initX"></span></p>
<p>moveX: <span id="moveX"></span></p>

<script type="text/javascript" src="http://apps.bdimg.com/libs/zepto/1.1.4/zepto.min.js"></script>
<script type="text/javascript" src="zepto.touchWipe.js"></script>
<script type="text/javascript">
  $(function() {
  $(&#39;.list-li&#39;).touchWipe({itemDelete: &#39;.btn&#39;});
  });

</script>
</body>
</html>
Copy after login

Effect:

Application effect in actual projects:

Eliminate BUG
Going to the above step, we have basically achieved the functions we need. ButThere are several problems:

1. The delete button on the right fails to click because span cannot bubble to the big button;

2 . A very serious problem. We added the touchmove event to p and used preventDefault() to block the original browser event. As a result, the page could not be scrolled when sliding p up or down!

The first problem is easier to solve. We directly remove the span and write "delete" into :before in the css, like this:

.itemWipe .item-delete:before {
  content: &#39;删除&#39;;
  color: #fff;
}
Copy after login

For the second problem, the Internet says to use iscroll to solve it. Here we refer to the sliding operation of contacts in mobile QQ.

General principle: At the beginning of sliding, determine whether the Y-axis moves more or the X-axis moves more. If the X-axis movement is large, it is judged to be a sliding delete operation, and we then use preventDefault();

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to PHP Chinese website!

Related recommendations:

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

jQuery and CSS3 imitation Petal Net fixed top position navigation menu with floating effect

The above is the detailed content of js imitates the operation of sliding the contact to the left and sliding out the delete button in QQ. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles