i am facing issue on jquery while using hover listener event, the code is just working on desktop while on mobile i can’t get it to work with the hover listener.So i just wanna implement click function instead of hover when the site is being displayed on mobile and hover on pc/laptop.I am sharing my code below. Also i am using blastjs and animate.css for text animation.
Header Element for the animation
JavaScript
x
2
1
<h2>About Me..</h2>
2
Jquery Code
JavaScript
1
21
21
1
$(document).ready(function () {
2
$("h2").blast({
3
delimiter: "character",
4
tag: "span",
5
});
6
7
var pageAbout = $(".about_wrapper");
8
9
$(".blast").on("hover", function () {
10
var el = $(this);
11
12
$(this).addClass("animated rubberBand");
13
$(this).one(
14
"webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",
15
function () {
16
el.removeClass("animated rubberBand");
17
}
18
);
19
});
20
});
21
Advertisement
Answer
You can check the device width before making the event listener
JavaScript
1
23
23
1
$(document).ready(function () {
2
$("h2").blast({
3
delimiter: "character",
4
tag: "span",
5
});
6
7
// You choose the event depending on the screen width
8
let event;
9
screen.width > 400 ? event = 'hover' : event = 'click'
10
//
11
var pageAbout = $(".about_wrapper");
12
13
$(".blast").on(event, function () {
14
var el = $(this);
15
16
$(this).addClass("animated rubberBand");
17
$(this).one(
18
"webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend",
19
function () {
20
el.removeClass("animated rubberBand");
21
}
22
);
23
}); });
If the screen.width was not suitable, you can find some ways to detect the device type on this discussion