javascript - Prevent hyperlink on touch and show hover state -
i'm not sure why when touch link on on site i'm building on iphone/tablet doesn't trigger hover on state , instead jumps straight through link. can see test site here http://lovelldesign.co.uk/home
the links big images , hover on state reveals name of project, that's why it's essential hover on state triggered first touch , user need touch again go through project.
i've searched touchstart examples on here , tried work them solve own problem no avail.
here html
<div class="projectcontainer"> <a href="<perch:content id="url" label="url" required="true" />" class="projectoverlay"> <h1><perch:content id="title" type="text" label="heading" required="true" /></h1> </a> <img src="<perch:content id="image" type="image" label="image" />" />
here css:
.projectcontainer { position: relative; max-width: 100%; overflow: hidden; margin-bottom: 7px; height: 100%; display: block; } .projectcontainer:hover .projectoverlay { opacity: 0.9; -moz-transition: 0.3s ease-out; -webkit-transition: 0.3s ease-out; -o-transition: 0.3s ease-out; transition: 0.3s ease-out; } .projectcontainer .projectoverlay { opacity: 0; background-color: #00152e; position: absolute; top: 0px; left: 0px; width: 96%; height: 100%; padding: 2%; color: white; text-decoration: none; }
any appreciated.
many thanks
i had same "issue" (actually it's correct behaviour) sometimes. fix it, need little of javascript.
first of all, let's detect touch events. can modernizr, minimum bundle achieve it: http://modernizr.com/download/#-touch-cssclasses-teststyles-prefixes
1) include generated script in <head>
section: modernizr going add "touch" class page's <html>
element if touch events supported, "no-touch" class if they're not.
2) if "touch" events enabled, need add javascript listener simulate hover state @ first click:
if(modernizr.touch){ var hoverclass = 'projectcontainer-hover', // "simulated hover" class $projectcontainers = $('.projectcontainer'); $projectcontainers.on('click',function(event){ var $this = $(this); if(!$this.hasclass(hoverclass)){ event.preventdefault(); $projectcontainers.removeclass(hoverclass); $this.addclass(hoverclass); return false; } }).on('focusout',function(){ // remove class if user focuses on element $(this).removeclass(hoverclass); }); }
i'm supposing you're using jquery. if you're not, , need support older ies, should know selecting elements class is not easy.
3) @ last, change css selector way:
.projectcontainer:hover .projectoverlay, .projectcontainer-hover .projectoverlay { // ...etc... }
note: method isn't perfect @ all, because touch events enabled in many laptops nowadays. device touch events enabled need double click if implement it. it's decide if it's worth it!
atm think alternative user-agent spoofing, really want avoid.
Comments
Post a Comment