How to detect touch hover using Javascript?
I have a problem with a Javascript plugin I am developing. This is a carousel plugin. The problem is how to determine when the carousel is touched over a period of time; like mouse hover. The issue is accompanied by the message "Uncaught TypeError: undefined is not a function".
http://jsfiddle.net/ospena/xDf4Z/20/
/*---------------------------------------------------------------------------------------------
@author Oscar Peña - @os-pena
@link ???
@github https://github.com/os-pena/Elegant-jQuery-Carousel-Slider
@version 0.0.2
@license ISC license
based on Simple-jQuery-Carousel-Slider : https://github.com/paulmason/Simple-jQuery-Carousel-Slider
----------------------------------------------------------------------------------------------*/
jQuery(function ($) {
// settings
var $slider = $('.slider'); // class or id of carousel slider
var $slide = 'li'; // could also use 'img' if you're not using a li
var $transition_time = 1000; // 1 second
var $time_between_slides = 2300; // 2.3 seconds
var $interval;
function slides() {
return $slider.find($slide);
}
slides().fadeOut();
// set active classes
slides().first().addClass('active');
slides().first().fadeIn($transition_time);
// auto scroll
function startloop() {
$interval = setInterval(
function () {
var $i = $slider.find($slide + '.active').index();
slides().eq($i).removeClass('active');
slides().eq($i).fadeOut($transition_time);
if (slides().length == $i + 1) $i = -1; // loop to start
slides().eq($i + 1).fadeIn($transition_time);
slides().eq($i + 1).addClass('active');
}, $transition_time + $time_between_slides);
}
function pauseLoop() {
window.clearInterval($interval);
}
$slider.hover(
function () {
pauseLoop(); // pause the loop
},
function () {
startloop(); //scroll()
});
$slide.addEventListener('touchstart', function(e){
alert ('paused');
e.preventDefault()
}, false);
$slide.addEventListener('touchend', function(e){
alert ('paused');
startloop();
e.preventDefault()
}, false)
return startloop();
});
+3
source to share