The previous article used the html5 touch event to create a simple sliding effect, achieving the effect of continuous page sliding up and down. Refer to the previous article and code html5 touch event to realize the up and down sliding of the touch screen page (1)
This article mainly achieves one effect: when the page slides to the top or bottom, it is prevented from continuing to slide;
Because the touch event is added to the page, the original default attributes of other pages such as scroll bars will not be changed. The demo in the previous article can slide up or down all the time, but the reality is that this is not advisable
Full code:
XML/HTML CodeCopy content to clipboard
-
>
-
<html lang="en">
-
<head>
-
<meta charset="UTF-8">
-
<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0"/>
-
<title>2014-5-4title>
-
<style>
-
* {margin: 0; padding: 0;}
-
#outer{ width:90%; height: 490px; 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;}
-
#spText{ height: 40px; width: 100%;border:1px solid red;}
-
h2{ width: 100%; text-align: center; }
-
h3{ width: 100%; padding-left:60%;}
-
style>
-
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js'>script>
-
head>
-
<body>
-
-
<div id="spText">div>
-
<div id="spText2">div>
-
<div id="outer">
-
<div id="inner">
-
<h2>背影h2>
-
<h3>—朱自清h3>
-
<p>我与父亲不相见已二年余了,我最不能忘记的是他的背影。[1-2]
-
那年冬天,祖母死了,父亲的差使也交卸了,正是祸不单行的日子。我从北京到徐州,打算跟着父亲奔丧回家。到徐州见着父亲,看见满院狼藉的东西,又想起祖母,不禁簌簌地流下眼泪。父亲说:“事已如此,不必难过,好在天无绝人之路!”[1-2]
-
回家变卖典质,父亲还了亏空;又借钱办了丧事。这些日子,家中光景很是惨淡,一半为了丧事,一半为了父亲赋闲。丧事完毕,父亲要到南京谋事,我也要回北京念书,我们便同行。[1-2]
-
到南京时,有朋友约去游逛,勾留了一日;第二日上午便须渡江到浦口,下午上车北去。父亲因为事忙,本已说定不送我,叫旅馆里一个熟识的茶房陪我同去。他再三嘱咐茶房,甚是仔细。但他终于不放心,怕茶房不妥帖;颇踌躇了一会。其实我那年已二十岁,北京已来往过两三次,是没有什么要紧的了。他踌躇了一会,终于决定还是自己送我去。I repeatedly advised him not to go; he just said: "It doesn't matter, it's not good for them to go!" [1-2]
-
We crossed the river and entered the station. I bought the ticket and he was busy taking care of the luggage. There was too much luggage, so I had to tip the porter to get through. He was busy negotiating the price with them again. I was really too smart at that time, and I always felt that what he said was not very nice and I had to interrupt myself. But he finally agreed on the price and sent me to the car. He picked me a chair by the car door; I spread the purple fur coat he made for me on the seat. He told me to be careful on the road and to be alert at night so as not to catch cold. I also asked the waiter to take good care of me. I secretly laughed at his pedanticness; they only recognized money, and trusting them was just free money! And for someone as old as me, can’t I still take care of myself? [1-2]
-
I said: "Dad, let's go." He looked outside the car and said, "I'm going to buy some oranges. You stay here, don't move around." I looked at the platform over there. There are several sellers waiting for customers outside the fence. When you get to the platform over there, you have to cross the railway, jump down and climb up again. My father is a fat man, so it would be more troublesome to walk there. I was going to go, but he refused, so I had to let him go. I saw him wearing a black cloth cap, a large black cloth mandarin jacket, and a dark blue cloth cotton robe. He staggered to the side of the railway and slowly leaned down. It was not a disaster. But it was not easy for him to cross the railway and climb to the platform over there. He clung to it with both hands and retracted his feet upwards; his fat body leaned slightly to the left, showing an effort. At this time I saw his back, and my tears flowed down quickly. I quickly wiped away my tears. Afraid that he would see it, and also afraid that others would see it. When I looked outside again, he had already hugged the scarlet orange and walked back. When crossing the railway, he first scattered the oranges on the ground, climbed down slowly, then picked up the oranges and walked away. When I got here, I quickly went to help him. He walked with me to the car and put oranges on top of my fur coat. So he puffed away the dirt on his clothes, feeling very relaxed. After a while he said: "I'm leaving. I'll write you a letter over there!" I watched him go out. He walked a few steps, turned around, saw me, and said, "Go in, there's no one inside." When his back blended in with the people coming and going, and he couldn't be found anymore, I came in and sat down, and my tears came again. . [1-2]
-
In recent years, my father and I have been running around, and the situation at home has deteriorated day by day. He went out to make a living as a young man, supported himself by himself, and did many great things. Who knew Laojing was so decadent! He was so sad that he couldn't help himself. When he is depressed in his heart, he will naturally express it outwardly; trivial matters in his family will often make him angry. He gradually treated me differently than before. But after not seeing each other for the past two years, he finally forgot about my faults and just thought about me and my son. [1-2]
-
After I came to the north, he wrote a letter to me, saying in the letter: "I am in good health, but my arm hurts terribly, and I have a lot of inconvenience in lifting chopsticks and pens. I think my death is not far away. "As I read this, in the glistening tears, I saw the fat figure in the green cloth cotton robe and black cloth mandarin again.well! I don’t know when I will see him again!p>
-
div>
-
div>
-
-
<script>
-
var startX, //Coordinates when touched
-
startY,
-
x, //sliding distance
-
y,
-
aboveY=0; // Set a global variable to record the position of the last internal block slide
-
var documentHeight=$("#inner").height();//The height of the internal sliding module
-
var wapperHeight=$("#outer").height(); //Height of the outer frame
-
-
var -
inner=document.getElementById("inner");
-
function touchStart(e){//Touch start -
e.preventDefault(); -
var -
touch=e.touches[0];
🎜> -
}
-
-
function touchMove(e){//Slide
-
e.preventDefault();
-
var touch = e.touches[0];
-
document.getElementById("spText").innerHTML=inner.style.top;
-
function touchEnd(e){//The finger leaves the screen
The sliding position is reflected in the global variable and must be converted into an integer using parseInt(); -
if(y>
0&- >
0){//Cannot be used when sliding to the top Swipe online -
//- inner.style.top
=- 0
; -
$("#inner").animate({top:0},200); -
aboveY=0
; -
-
if(y- <0&&(aboveY<(-( documentHeight-wapperHeight)))){//When sliding to the bottom, it cannot be slid offline
- $("#inner").animate({top:-(documentHeight-wapperHeight)},200);
aboveY- =-(documentHeight-wapperHeight);
-
}//
document.getElementById("outer").addEventListener('touchstart', touchStart,false); -
document.getElementById("outer").addEventListener('touchmove', touchMove,false); -
document.getElementById("outer").addEventListener('touchend', touchEnd,false); -
script- >
-
body>
-
html- >
-
The above is the complete code of the demo. This time I used jquery because animate is used to make the sliding to the top and bottom a little flexible
The following is the demo effect. You can also copy the code and access it on your mobile phone. The code is relatively simple and only achieves the effect without considering compatibility and robustness
The above is the entire content of this article, I hope it will be helpful to everyone’s study.
Original text:
http://www.cnblogs.com/leinov/p/3707197.html