


hwSlider-content sliding switching effect (2): responsive touch sliding
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.
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}
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(); }); });
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(); } } });
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

With the popularity of mobile devices, web design needs to take into account factors such as device resolution and screen size of different terminals to achieve a good user experience. When implementing responsive design of a website, it is often necessary to use the image carousel effect to display the content of multiple images in a limited visual window, and at the same time, it can also enhance the visual effect of the website. This article will introduce how to use CSS to achieve a responsive image automatic carousel effect, and provide code examples and analysis. Implementation ideas The implementation of responsive image carousel can be implemented through CSS flex layout. exist

A tutorial on using CSS to implement a responsive sliding menu requires specific code examples. In modern web design, responsive design has become an essential skill. To accommodate different devices and screen sizes, we need to add a responsive menu to the website. Today, we will use CSS to implement a responsive sliding menu and provide you with specific code examples. First, let's take a look at the implementation. We will create a navigation bar that automatically collapses when the screen width is smaller than a certain threshold and expands by clicking the menu button.

How to use HTML, CSS and jQuery to create a responsive tag cloud. A tag cloud is a common web element used to display various keywords or tags. It usually displays the importance of keywords in different font sizes or colors. In this article, we will introduce how to use HTML, CSS and jQuery to create a responsive tag cloud, and give specific code examples. Creating the HTML Structure First, we need to create the basic structure of the tag cloud in HTML. You can use an unordered list to represent tags

How to use HTML, CSS and jQuery to create a responsive scrolling notification bar. With the popularity of mobile devices and the increase in user requirements for website access experience, designing a responsive scrolling notification bar has become more and more important. Responsive design ensures that the website displays properly on different devices and that users can easily view notification content. This article will introduce how to use HTML, CSS and jQuery to create a responsive scrolling notification bar, and provide specific code examples. First we need to create the HTM

How to use JavaFX to build a responsive UI interface in Java9 Introduction: In the development process of computer applications, the user interface (UI) is a very important part. A good UI can improve the user experience and make the application more attractive. JavaFX is a graphical user interface (GUI) framework on the Java platform. It provides a rich set of tools and APIs to quickly build interactive UI interfaces. In Java 9, JavaFX has become a JavaSE

How to create a responsive carousel layout using HTML and CSS Carousels are a common element in modern web design. It can attract the user's attention, display multiple contents or images, and switch automatically. In this article, we will introduce how to create a responsive carousel layout using HTML and CSS. First, we need to create a basic HTML structure and add the required CSS styles. The following is a simple HTML structure: <!DOCTYPEhtml&g

How to use React to develop a responsive backend management system. With the rapid development of the Internet, more and more companies and organizations need an efficient, flexible, and easy-to-manage backend management system to handle daily operations. As one of the most popular JavaScript libraries currently, React provides a concise, efficient and maintainable way to build user interfaces. This article will introduce how to use React to develop a responsive backend management system and give specific code examples. Create a React project first

Tips for Responsive Website Development with Webman In today’s digital age, people are increasingly relying on mobile devices to access the Internet. In order to provide a better user experience and adapt to different screen sizes, responsive website development has become an important trend. As a powerful framework, Webman provides us with many tools and technologies to realize the development of responsive websites. In this article, we will share some tips for using Webman for responsive website development, including how to set up media queries,
