Javascript Gallery sliders that can be touch/swiped on mobile AND clicked and dragged on desktop?
After googling for a bit I answered my own question. Here's a good site http://www.iosscripts.com/iosslider/. The swiping/clicking works great on both my Mac laptop with 10.7.4 and on my iPhone 4 with iOS4.
Adding touch-swipe support to jquery based sliders
It is possible without switching to jQuery Mobile. I did so in this small project for school.
The way it's done is by using this library called jQuery UI Touch Punch which will simulate mouse events from touch devices. All you need to do is include the library under your jQuery includes like such: <script src="jquery.ui.touch-punch.min.js"></script>
.
Add swipe support to JS slider
You can implement your own swipe functions with touchstart
and touchend
events. (It's not supported on desktop safari right now, but you can use mousedown
and mouseup
events on desktop).
Just add some eventlisteners to you element and watch the event pageX
property.
let xPos;
const touchStart = (event) => {
xPos = event.pageX;
};
const touchEnd = (event) => {
if (event.pageX < xPos) {
// you swiped left
} else {
// you swiped right
}
};
window.addEventListener('touchstart', touchStart);
window.addEventListener('touchend', touchEnd);
Also you can use the touchmove
event too for more complex and more advanced swipe implementation.
jQuery UI slider Touch & Drag/Drop support on Mobile devices
jQuery ui doesn't have touch support. You should use it with jQuery-ui touch punch.
Just add the script after jQuery ui:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.21/jquery-ui.min.js"></script>
<script src="jquery.ui.touch-punch.min.js"></script>
You can also use cdnjs:
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
Note: Better give this repo a star on Github.
Can anybody recommend a slider a horizontal slider or carousel with variable image width support and touch support?
I would highly recommend Swiper.
http://www.idangero.us/sliders/swiper/
Check out their multi-width demo down on this page - the demo is titled 'Different Widths'
http://www.idangero.us/sliders/swiper/demos.php
This slider works beautifully on touch devices and has an awesome API!
Related Topics
Grabbing Style.Display Property via Js Only Works When Set Inline
Flexslider <Li> Height's Equal the Tallest <Li> on a Mobile Browser
Displaying a Div Only on Tumblr Blog Homepage
Pause/Resume CSS Animations When Switching Tabs
Jquery .Css() Function Not Returning Expected Values
D3 Bar Chart Not Showing First Element in Array
How to Start CSS3 Animations at a Specific Spot
Using 'Order' Property to Position Flex Item Between Siblings
Disable (Exclude) Bootstrap for One Div on a Page
Angular - Ngfor* Four Elements on Row
Overflow:Auto Not Working in Touch Devices(Ios)
How to Detect Which CSS Grid Column and Row an Element Is in Using JavaScript
How to Reasonably Animate Font Awesome Lock Icons
Saving Dropdown Menu Selection in a Cookie
Svg Resizes on Hover in Safari Only
JavaScript - Page Has to Be Refreshed to Show Particle-Slider Logo Effect