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

hwSlider-content sliding switching effect (2): responsive touch sliding

黄舟
Release: 2017-03-24 10:40:45
Original
2029 people have browsed it

Today we continue to explain the second part of the content sliding switching effect. Nowadays, our web development must adapt to mobile devices, which means that our web pages must be accessible on mobile devices such as mobile phones. Therefore, I enhanced the basic switching effect of the first part and added responsiveness and touch sliding. Effect.

hwSlider-content sliding switching effect (2): responsive touch sliding
View the demo download source code

This article is the second part of hwSlider-content sliding switching effect. The demonstration DEMO is based on the first part of the content, so, If you haven’t read the first part, please refer to: hwSlider-content sliding switching effect (1)

Responsiveness

What is responsive design, here I I won’t describe it. In hwSlider, we set the width and height of the slider through CSS, and set the width in percentage.

[code=css]
#hwslider{width: 100%;height:auto;min-height: 120px;margin:40px auto; position: relative; overflow: hidden;}
#hwslider ul{width: 100%; height:100%; position: absolute; z-index: 1}
#hwslider ul li{display:none;position:absolute; left:0; top:0; width: 100%;height:100%; overflow: hidden;}
#hwslider ul li.active{display: block;}
#hwslider ul li img{max-width: 100%}
#dots{position: absolute; bottom:20px; left:200px; min-width:60px; height: 12px; z-index: 2;}
#dots span{float: left; width:12px;height: 12px; border: 1px solid #fff; border-radius: 50%; background: #333; margin-right: 8px; cursor: pointer;}
#dots span.active{background:orangered}
.arr{display:none;position: absolute; top: 140px; z-index: 2;width: 40px; height: 40px; line-height: 38px; text-align: center;; font-size: 36px; background: rgba(0,0,0,.3); color: #fff; text-decoration: none}
.arr:hover{background: rgba(0,0,0,.7); text-decoration: none;}
#hwslider:hover .arr{display: block; text-decoration: none;color: #fff}
#prev{left: 20px}
#next{right: 20px}
Copy after login

Next, we define variables at the beginning of the js part. In the initialization resize() function, we calculate and position the positions of the navigation dots and navigation arrows, and adjust the browser window size resize() is also called when.

[code=js]
$(function(){
	var slider = $("#hwslider");
	var dots = $("#dots span"),
		prev = $("#prev"),next = $("#next");
	var sliderInder = slider.children('ul')
	var hwsliderLi = sliderInder.children('li');
	var hwsliderSize = hwsliderLi.length;  //滑块的总个数
	var sliderWidth = 600; //滑块初始宽度
	var sliderHeight = 320; //滑块初始高度
	var index = 1; //初始显示第一个滑块
	var speed = 400; //滑动速度
	var interval = 3000; //间隔时间
	var dotShow = true; //是否显示可切换的导航圆点
	var autoPlay = false; //是否支持自动滑动
	var clickable = true; //是否已经点击了滑块在做滑动动画

	

	//初始化组件
	var resize = function(){
		var sWidth = slider.width();
		var dotWidth = hwsliderSize*20;
		var dotOffset = (sWidth-dotWidth)/2;

		var sHeight = sliderHeight/sliderWidth*sWidth;
		slider.css('height',sHeight); 
		$("#dots").css('left',dotOffset+'px'); //导航圆点位置

		var arrOffset = (sHeight-40)/2;
		$(".arr").css('top',arrOffset+'px'); //导航箭头位置
	}

	resize();

	$(window).resize(function(){
	  resize();
	});

});
Copy after login

Mobile touch screen sliding

On mobile devices, we can touch the screen and use gesture sliding to switch the slider. To achieve this effect, the core touch event needs to be used. Handling touch events can track each finger sliding on the screen.

The following are four touch events:

touchstart: triggered when the finger is placed on the screen

touchmove: triggered when the finger slides on the screen

touchend : Triggered when the finger leaves the screen

touchcancel: Triggered when the system cancels the touch event. This seems to be less used

Well, now we need to bind the listener for touch events on the slider , obtain the position of the finger on the screen slider at touchstart and touchend respectively, and then determine whether to slide left or right based on the displacement, and then call moveTo() to implement sliding switching.

[code=css]
var mouseX = 0,
	touchStartY = 0,
	touchStartX = 0;

	hwsliderLi.on({
		//触控开始
		'touchstart': function(e) {
			touchStartY = e.originalEvent.touches[0].clientY;
			touchStartX = e.originalEvent.touches[0].clientX;
		},

		//触控结束
		'touchend': function(e) {
			var touchEndY = e.originalEvent.changedTouches[0].clientY,
				touchEndX = e.originalEvent.changedTouches[0].clientX,
				yDiff = touchStartY - touchEndY,
				xDiff = touchStartX - touchEndX;

			if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {
		        if ( xDiff > 5 ) {
		        	if(index >= hwsliderSize){
						index = 1;
					}else{
						index += 1;
					}
		            moveTo(index,'next');
		        } else {
		        	if(index == 1){
						index = hwsliderSize;
					}else{
						index -= 1;
					}
		            moveTo(index,'prev');
		        }                       
		    }
			touchStartY = null;
			touchStartX = null;
		},

		//触控移动
		'touchmove': function(e) {
			if(e.preventDefault) { e.preventDefault(); }

		}
	});
Copy after login

Coupled with the basic slider js code in the previous article, a responsive touch-sliding content sliding effect can be achieved.
If you want to implement dragging and sliding on PC, you need to bind the onmousedown, onmousemove and onmouseup events of the slider to realize sliding switching by holding down the mouse and dragging the slider. The main code will not be posted here. You can Download the source code to view.

The above is the content of hwSlider-content sliding switching effect (2): responsive touch sliding. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Related articles:

Examples help you understand the HTML5 sliding area selection element Slider element

hwSlider-Content sliding switching effect (1)

Detailed introduction to the WeChat applet slider component

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!