Specific hover for touch

0

Is there any CSS Hover for touch? Or way to emulate one with javascript that only activates if it is touch?

    
asked by anonymous 05.07.2018 / 02:09

1 answer

0

My solution was this:

window.onload = function(e){
	x = document.getElementById('x');
	
	x.addEventListener("touchstart", touch);
	x.addEventListener("touchend", touch);
	x.addEventListener("mousemove", mouse);
	x.addEventListener("mouseout", mouse);
	
	function touch(event){
		if(event.type == 'touchstart'){ 
            setTimeout(function(){ 
			x.style.background = 'blue';
            }, 200);
		}
		if(event.type == 'touchend'){
			setTimeout(function(){ 
				x.style.background = 'red'; 
			}, 3000);
		}
	}
	function mouse(event){
		if(event.type == 'mousemove'){ 
			x.style.background = 'blue';
		}
		if(event.type == 'mouseout'){
			x.style.background = 'red';
		}
	}
}
#x{
	height: 300px;
	width: 300px;
	background: red;
}
<div id="x"></div>
    
05.07.2018 / 03:19