Mobile and tablet versions loose image hover effects :-(

Hi from 15 degrees C York Uk :smile:

http://codepen.io/Pingbat/pen/xbBQqE

The images in the desktop version behave exactly how i want but when the site is scaled down to tablet and mobile size all cs styling is ignored and horrible things happen like bullets next the thumb images and large images appearing without the need to mouse over.

Now whilst I am a noob with css here is what I think is going on… The site has three css instructions for the size of the viewport. The CSS lines that control the image hover over styling begins on CSS line 245. But that code (ul enlarge etc) needs to be copied and pasted into line 41 etc.

This screen grab illustrates my problem -


Any pointers just to get me in the right direction is much appreciated.

Thanks,
David

You’re right. You need to move this set of lines out of the media query css. I would move it out of the media query rather than having two places to have to remember to keep consistent.

Media query rules are only applied if certain conditions apply, so you need to be aware of that when marking up your css…Another is you only want to have rules in one place, and only duplicate if you need to override in certain situations (i.e. in media queries)

ul.enlarge {
	text-align: right;
	list-style-type: none; /*remove the bullet point*/
}
1 Like

It may also be worth mentioning that touch-screen devices don’t generally support hover.

1 Like

You are designing using the mobile first approach which generally means you should be designing the mobile look first. Any rules you need for mobile will be at the start of the css and those rules will inherit into the large screen sizes if required.

Don’t design ‘mobile first’ and then design the desktop first because you won’t have yet built the basic rules you need yet (such as hiding the bullets etc). You can do it that way but you need to ensure that general rules are available for mobile and that you aren’t adding them direct into the larger media queries.

If you want to design desktop first then its best to use max-width media queries and design the desktop first and then make the changes for mobile.

Mobile first (usualy) means designing the mobile look first and then changing the look with media queries for larger devices.

1 Like

Thank you so much DaveMaxwell, that worked :slight_smile:

Oh dear :frowning: If you get chance… "how do i code for a similar on hover / touch effect to work on an iPad for example?

Hi PaulOB, your right I know I should follow that principal, I think my skill level means often I dont see problems till later on, fingers crossed It will be OK!

If you use anchors then the ipad will assume a first touch is a hover and show your image. The problem arises in that the the image won’t go away until you touch somewhere else which usually means blocking part of the page (sometimes this works when the trigger item is not an anchor but a child item is an anchor).

Here’s an example:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.menu {
	margin:0;
	padding:0;
	list-style:none;
	position:relative;
}
.menu li a {
	height:200px;
	border:1px solid green;
	display:block;
}
.image {
	position:absolute;
	left:-999em;
	width:200px;
	height:200px;
	background:red;
}
.menu li:hover{cursor:pointer}
.menu li:hover span, 
.menu li.over span {
	left:0;
}
</style>
</head>

<body>
<ul class="menu">
		<li><a href="#"><span class="tnail">Thumbnail</span> <span class="image">img</span></a> </li>
		<li><a href="#"> <span class="tnail">Thumbnail</span> <span class="image">img</span></a> </li>
		<li><a href="#"> <span class="tnail">Thumbnail</span> <span class="image">img</span></a> </li>
		<li><a href="#"> <span class="tnail">Thumbnail</span> <span class="image">img</span></a></li>
		<li><a href="#"> <span class="tnail">Thumbnail</span> <span class="image">img</span></a> </li>
</ul>

</body>
</html>

A safer alternative is to add some js to monitor touch and add a class at touchstart/touchend and duplicate the hover rules.

Here’s a live example.

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <style>
    .menu {
    	margin:0;
    	padding:0;
    	list-style:none;
    	position:relative;
    }
    .menu li {
    	height:200px;
    	width:200px;
    	border:1px solid green
    }
    .image {
    	position:absolute;
    	left:-999em;
    	width:200px;
    	height:200px;
    	background:red;
    }
    .no-touch .menu li:hover span{left:0}/* for non touch devices*/
    .menu li.over span {/* for touch devices */
    	left:0;
    }
    </style>
    </head>
    <!-- class added to body so that the hover isn't applied when on mobile  as it messes with the touchstart events -->
    <body class="no-touch">
    <ul class="menu">
    		<li> <span class="tnail">Thumbnail</span> <span class="image">img</span> </li>
    		<li> <span class="tnail">Thumbnail</span> <span class="image">img</span> </li>
    		<li> <span class="tnail">Thumbnail</span> <span class="image">img</span> </li>
    		<li> <span class="tnail">Thumbnail</span> <span class="image">img</span> </li>
    		<li> <span class="tnail">Thumbnail</span> <span class="image">img</span> </li>
    </ul>
    <script  type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
    <script>
    $(document).ready(function() {
        $('.menu li').bind('touchstart touchend', function(e) {
            e.preventDefault();
            $(this).toggleClass('over');
    				$('body').removeClass('no-touch');
        });
    });
    </script>
    </body>
    </html>

A class of no-touch is added to the body.

<body class="no-touch">

This is then used to trigger the normal hover rules.

 .no-touch .menu li:hover span{left:0}/* for non touch devices*/

The no-touch class is then removed with a script that monitors touch events and at the same time we add (and then remove) a class to the items when touched (touchstart and touchend).

  $(document).ready(function() {
            $('.menu li').bind('touchstart touchend', function(e) {
                e.preventDefault();
                $(this).toggleClass('over');
       	 $('body').removeClass('no-touch');
            });

The reason that we apply no-touch class to the body is that once the touch events are added to the page the hover effects seem to start working on the touch device and ruin the effect because they don’t ‘un-hover’.

When a user touches the element a class of .over is added so we just duplicate our hover rules.

.menu li.over span {/* for touch devices */
    	left:0;
    }

It’s pretty straight forward.

These days I usually avoid hover for important items and use click instead using js (e.g. dropdown menus) and let all devices have a click effect instead.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.