I want to create a div that shows another div on hover on desktop but on-click on mobile.
Is this possible?
I want to create a div that shows another div on hover on desktop but on-click on mobile.
Is this possible?
Following code may help you..
var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());
var isWindows = /windows phone/i.test(navigator.userAgent.toLowerCase());
var isBlackberry = /blackberry/i.test(navigator.userAgent.toLowerCase());
var isiDevice = /ipad|iphone|ipod/i.test(navigator.userAgent.toLowerCase());
if(isAndroid || isWindows || isBlackberry || isiDevice){
$('#element').on('click',function(){
//your code here
});
}else{
$('#element').on('hover',function(){
//your code here
});
}
Or you can see the following post...
http://www.jquery4u.com/mobile/detect-mobile-devices-jquery/
if screen size mobile{
$("#element).click(some function)
}
else if screen size desktop{
$("#element).hover(some function)
}
or the other way of workaround
$("#element").click(function(){
if(screen size is mobile){
//do something (show div)
}
else if(screen size is desktop){
//do something
}
});
$("#element").hover(function(){
if(screen size is mobile){
//do something
}
else if(screen size is desktop){
//do something (show div)
}
});
combining Sixin and CSS TRICKS answer below:
if ($(window).width() < 960) {
$("#element").click(function(){
var e = document.getElementById("foo");
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
});
}
else {
$("#element").hover(function(){
var e = document.getElementById("foo");
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
});
}