Home  >  Article  >  Web Front-end  >  Detailed explanation of Js judging the up and down sliding direction of H5

Detailed explanation of Js judging the up and down sliding direction of H5

小云云
小云云Original
2018-01-18 17:00:542944browse

This article mainly shares with you a JS sample code for judging the up and down sliding direction of H5 and sliding to the top and bottom. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Swipe up to hide the bottom floating box, slide down to display the floating box. To view using PC browser, please set the browser to mobile browser mode.


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>手机端触屏手指滑动方向</title>
<meta content="width=device-width,initial-scale=1.0,minimum-scale=1.0,user-scalable=no,maximum-scale=1.0" id="viewport" name="viewport">
<script type="text/javascript">
var currntY = 0;
var lastY = 0;
var touch_screen = {
   //方向
  direction: {
	direction: "",
	object: null,

	start: function() {
      var self = this,
        obj = self.object;
      obj.addEventListener(&#39;touchstart&#39;, function(e) {
        self.move();
      }, false);
      obj.addEventListener(&#39;touchend&#39;, function(e) {
		//self.move();	
		lastY = document.body.scrollTop;	
      }, false);
    },
    //拖动滑动时
    move: function() {
      var self = this;	  
	  self.object.addEventListener(&#39;touchmove&#39;, function(e) {
		currntY = document.body.scrollTop;
        var direct = currntY - lastY;
		
        if (direct > 0) {
          self.direction = "down";
		  document.getElementById("nav").style.visibility="hidden";//隐藏
		} else if (direct < 0) {
		  self.direction = "up";
		  document.getElementById("nav").style.visibility="visible";//显示
		} 

		if(currntY == 0){
		  self.direction = "top";
		} else if((currntY + window.screen.availHeight) == document.body.clientHeight){
		  self.direction = "bottom";
		  document.getElementById("nav").style.visibility="visible";//显示
		}
        
        document.getElementById(&#39;nav&#39;).innerHTML= self.direction;
        //document.getElementById(&#39;nav&#39;).innerHTML= currntY + "|" + window.screen.availHeight + "|" + document.body.clientHeight;
		lastY = document.body.scrollTop;
      }, false);      
    },
    
    //通过一个dom对象进行初始化
    init: function(a) {
      var class_clone = function(source) { 
        var result={};
        for (var key in source) {
          result[key] = typeof source[key]==="object" ? class_clone(source[key]) : source[key];
        } 
        return result; 
      }
      var self = class_clone(touch_screen.direction);
      self.object = document.getElementById(a);
      if (!self.object) {
        alert(&#39;bind_object is not an object&#39;);
        return false;
      }
      self.start();
    }
  }
}

//页面加载完成
window.onload = function() {
  touch_screen.direction.init("inner");
}
</script>

<style> 
  * {margin: 0; padding: 0;}  
  #outer{ width:90%; height: 100%; background: #000; margin: auto; overflow: hidden;}  
  #inner{width:80%; background: #e4e4e4; margin: auto; position:relative; top:0px; font-size: 1em; padding: 30px 10px; }  
  #inner p{line-height: 30px; letter-spacing: 3px; text-indent:2em;} 
  #nav { width:100%; height: 50px; border: 1px solid #D4CD49; background: #947674;text-align: center; position:fixed;left:0;bottom:0; } 
  #nav2 { width:100%; height: 50px; border: 1px solid #D4CD49;background: #947674;} 
  h2{ width: 100%; text-align: center; }  
  h3{ width: 100%; padding-left:60%;}  
</style> 
</head>

<body>
<p id="spText2"></p> 
  <p id="outer"> 
    <p id="inner"> 
      <h2>背影</h2> 
      <h3>—朱自清</h3> 
            <p>我与父亲不相见已二年余了,我最不能忘记的是他的背影。 
    那年冬天,祖母死了,父亲的差使也交卸了,正是祸不单行的日子。我从北京到徐州,打算跟着父亲奔丧回家。到徐州见着父亲,看见满院狼藉的东西,又想起祖母,不禁簌簌地流下眼泪。父亲说:“事已如此,不必难过,好在天无绝人之路!”  
    回家变卖典质,父亲还了亏空;又借钱办了丧事。这些日子,家中光景很是惨淡,一半为了丧事,一半为了父亲赋闲。丧事完毕,父亲要到南京谋事,我也要回北京念书,我们便同行。 
    到南京时,有朋友约去游逛,勾留了一日;第二日上午便须渡江到浦口,下午上车北去。父亲因为事忙,本已说定不送我,叫旅馆里一个熟识的茶房陪我同去。他再三嘱咐茶房,甚是仔细。但他终于不放心,怕茶房不妥帖;颇踌躇了一会。其实我那年已二十岁,北京已来往过两三次,是没有什么要紧的了。他踌躇了一会,终于决定还是自己送我去。我再三回劝他不必去;他只说:“不要紧,他们去不好!” 
    我们过了江,进了车站。我买票,他忙着照看行李。行李太多了,得向脚夫行些小费才可过去。他便又忙着和他们讲价钱。我那时真是聪明过分,总觉他说话不大漂亮,非自己插嘴不可,但他终于讲定了价钱;就送我上车。他给我拣定了靠车门的一张椅子;我将他给我做的紫毛大衣铺好座位。他嘱咐我路上小心,夜里要警醒些,不要受凉。又嘱托茶房好好照应我。我心里暗笑他的迂;他们只认得钱,托他们只是白托!而且我这样大年纪的人,难道还不能料理自己么?  
    我说道:“爸爸,你走吧。”他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。 
    近几年来,父亲和我都是东奔西走,家中光景是一日不如一日。他少年出外谋生,独力支持,做了许多大事。哪知老境却如此颓唐!他触目伤怀,自然情不能自已。情郁于中,自然要发之于外;家庭琐屑便往往触他之怒。他待我渐渐不同往日。但最近两年不见,他终于忘却我的不好,只是惦记着我,惦记着我的儿子。 
    我北来后,他写了一信给我,信中说道:“我身体平安,惟膀子疼痛厉害,举箸提笔,诸多不便,大约大去之期不远矣。”我读到此处,在晶莹的泪光中,又看见那肥胖的、青布棉袍黑布马褂的背影。唉!我不知何时再能与他相见! </p> 
<p id="nav" style="color:#F00; font-size:35px"></p>
    </p> 
  </p> 
<p id="nav2" ></p>
</body>
</html>

Related recommendations:

Based on jquery to implement the background left menu click to slide up and down to display_jquery

html5 touch event realizes sliding up and down the touch screen page (2)_html5 tutorial skills

CSS3 study notes (4)-swipe up and down to expand the button_html/css_WEB-ITnose

The above is the detailed content of Detailed explanation of Js judging the up and down sliding direction of H5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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