1) up and down rolling rebound
function swipe () {
Demand:
1. Get the parent of UL and UL to scroll
var ulfather = Document.queryselector ('. container ');
var ul = ul.queryselector (' ul ');
2. Get scrolling normal range
var starty = 0;
var movey = 0;
Distance to scroll
var dist = 0;
Current UL scroll position
var curdisy = 0;
Normal range when scrolling down
var normalscrolldown = 0;
Normal range when scrolling up
var normalscrollup = left.offsetheight-ul.offsetheight;
Maximum range when scrolling down
var maxscrolldown = + Normalscrolldown;
Maximum range when scrolling up
var maxscrollup = normalScrollUp-100;
Left.addeventlistener (' Touchstart ', function (e) {
Starty = E.touches[0].clienty;
})
Left.addeventlistener (' Touchmove ', function (e) {
Movey = E.touches[0].clienty;
Disty = Movey-starty;
Allows scrolling if the current scroll position is within the maximum scrolling range
if (Curdisty + disty < maxscrolldown && curdisty + disty > Maxscrollup) {
No action in normal range
Ul.style.transition = "None";
Ul.style.transform = "Translatey (" + (Disy+curdisy) + "px)";
}
})
Left.addeventlistener (' Touchend ', function (e) {
Record the current UL scroll position
Curdisty = Curdisty + disty;
Set the normal range if the current scroll position exceeds the range
if (Curdisty > Normalscrolldown) {
Curdisty = Normalscrolldown;
Increase animation effect
Ul.style.transition = "All 0.4s";
Ul.style.transform = "Translatey (" + (Curdisy) + "px)";
}
if (Curdisty < Normalscrollup) {
Curdisty = Normalscrollup;
Ul.style.transition = "All 0.4s";
Ul.style.transform = "Translatey (" + (Curdisy) + "px)";
}
})
}
*****************************************************************************************
Use object-oriented encapsulation of a function that is rolled up and down
/**
* Created by ZHOUSG on 2016/3/18.
*/
/**
* @author ZHOUSG
* @Date 2016-02-04
* @Method sliding method for a container inside a large container to do a sliding package
* @param args Args.swipedom Large container object Args.swipetype sliding type args.swipedistance buffer distance
* Note: The high width of the sub-container is the height of the content to be taken, so the size of the padding has an effect
*/
if (!window.itcast) {
Window.itcast = {};
}
Itcast.iscroll = function (args) {
/* not initialized at the time of invocation is initialized once */
if (! ( This instanceof Arguments.callee)) return new Arguments.callee (args);
This.init (args);
};
Itcast.iScroll.prototype = {
Constructor:itcast.iScroll,
Init:function (args) {
/* local variable to accept the current this*/
var = this;
/* If the incoming object is a DOM object, think of him as our big container box */
if (args.swipedom && typeof args.swipedom = = ' object ') {
That.parentdom = Args.swipedom;
}
/* Stop initializing if no parent container exists */
if (!that.parentdom) return false;
/* Find child containers */
That.childdom = that.parentdom.children&&that.parentdom.children[0]?that.parentdom.children[0]: ";
/* Stop initializing if no child container exists */
if (!that.childdom) return false;
/* Initialize the passed in Parameters */
That.settings = {};
/* Default type Default y-axis slide if not Y, start with the x-axis */
That.settings.swipeType = args.swipetype?args.swipetype: ' Y ';
/* Default buffer sliding distance */
That.settings.swipeDistance = args.swipedistance>=0?args.swipedistance:150;
/* Initialize Slide */
That._scroll ();
},
/* How to set up a location in the open * *
Settranslate:function (Translate) {
This.currpostion = translate;
This._addtransition ();
This._changetranslate (this.currpostion);
},
_addtransition:function () {
This.childDom.style.transition = "all. 2s ease";
This.childDom.style.webkitTransition = "all. 2s ease";/* compatible with old version WebKit kernel browser */
},
_removetransition:function () {
This.childDom.style.transition = "None";
This.childDom.style.webkitTransition = "none";/* compatible with older version WebKit kernel browser */
},
_changetranslate:function (Translate) {
if (This.settings.swipeType = = ' Y ') {
This.childDom.style.transform = "Translatey (" +translate+ "px)";
This.childDom.style.webkitTransform = "Translatey (" +translate+ "px)";
}else{
This.childDom.style.transform = "TranslateX (" +translate+ "px)";
This.childDom.style.webkitTransform = "TranslateX (" +translate+ "px)";
}
},
_scroll:function () {
/* local variable to accept the current this*/
var = this;
/* Type of swipe */
var type = That.settings.swipeType = = ' y '? True:false;
/* height or width of parent container */
var parentheight = type?that.parentdom.offsetheight:that.parentdom.offsetwidth;
/* Height or width of child container */
var childheight = type?that.childdom.offsetheight:that.childdom.offsetwidth;
/* Child containers when no parent container is large */
if (Childheight < parentheight) {
if (type) {
That.childDom.style.height = Parentheight + ' px ';
Childheight = parentheight;
}else{
That.childDom.style.width = parentheight + ' px ';
Childheight = Parentheight;
}
}
/* Buffer distance */
var distance = that.settings.swipeDistance;
/* interval */
/* Left box positioning interval */
that.maxpostion = 0;
th At.minpostion =-(childheight-parentheight);
/* Sets the current position of the slide */
That.currpostion = 0;
that.startpostion = 0;
that.endpostion = 0;
that.movepostion = 0;
/*1. Slide */
That.childDom.addEventListener (' Touchstart ', function (e) {
/* initial y coordinates */
That.startpostion = Type?e.touches[0].clienty:e.touches[0].clientx;
},false);
That.childDom.addEventListener (' Touchmove ', function (e) {
E.preventdefault ();
/* Record the value of the Endy while doing the swipe */
That.endpostion = Type?e.touches[0].clienty:e.touches[0].clientx;
That.movepostion = that.startpostion-that.endpostion;/* Calculates the distance traveled */
/*2. Sliding interval */
/* is the sliding interval */
if (that.currpostion-that.movepostion) < (that.maxpostion+distance)
& &
(that.currpostion-that.movepostion) > (that.minpostion-distance)) {
that._removetransition ();
That._changetranslate (that.currpostion-that.movepostion);
}
},false);
Window.addeventlistener (' Touchend ', function (e) {
/* Re-calculates the current position after limiting the sliding interval */
/* To determine if within our reasonable positioning interval */
/* Swipe down first */
if ((that.currpostion-that.movepostion) > That.maxpostion) {
That.currpostion = that.maxpostion;
that . _addtransition ();
That._changetranslate (that.currpostion);
}
/* when you want to swipe up */
Else if ((that.currpostion-that.movepostion) < that.minpostion) {
That.currpostion = That.minpostion;
That._addtransition ();
That._changetranslate (that.currpostion);
}
/* Normal condition */
else{
that.currpostion = that.currpostion-that.movepostion;
}
That._reset ();
},false);
},
_reset:function () {
var = this;
that.startpostion = 0;
that.endpostion = 0;
that.movepostion = 0;
}
};
How to use:
1, first introduced
2. Call
Itcast.isscroll ({
Gets the parent of a scrolling element
SwipeDom:document.querySelector ('. Content '),
If it is a horizontal scroll x, scroll vertically y
Swipetype: ' Y ',
Rebound distance
swipedistance:100
})
*****************************************************************************************
2) package Mobile-side Click events
Because the Click event has a 300ms delay on the mobile side
Demand:
1, the occurrence time of the event should be within 150ms
2. No Touchmove event triggered
function Tap (dom,callback) {
var ismove = false;
var startTime = 0;
Dom.addeventlistener (' Touchstart ', function () {
Returns the number of milliseconds for the current event
StartTime = Date.now ();
})
Dom.addeventlistener (' Touchmove ', function () {
Ismove = true;
})
Dom.addeventlistener (' Touchend ', function () {
if (!ismove && date.now ()-starttime < 150) {
Callback && callback ();
}
Ismove= false;
StartTime = 0;
})
}
Mobile End Action