4

I'm currently using the following code below using jQuery and the hover function to fade in a the 'caption' element when the user hovers over the image. This works perfectly on desktop browsers, however when testing it with mobile touch devices such as iPad which requires the user to tap the image to trigger the hover function the caption fades in as expected but stays active until the user selects another object on the page.

I would like to know a simple way to modify my javascript code to detect mobile touch devices and then put some sort or timer to the caption so that it fades back automatically after a period of time?

<!-- include jQuery library -->
<script type="text/javascript" src="./_js/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

    //On mouse over those thumbnail
    $('.item-caption').hover(function() {

        //Display the caption
    $(this).find('.caption').stop(false,true).fadeIn(200);
    },
    function() {    

    //Hide the caption
    $(this).find('.caption').stop(false,true).fadeOut(600);
});

});
</script>

</head>
<body>

    <div class="item-caption"><a href="http://www.domain.com">
        <div class="caption">   
            <ul>
                <li><h2>TITLE</h2></li>
                <li><h3>Description</h3></li>
            </ul>       
        </div>
        <img src="./_img/example_image.jpg"></a>
    </div>

</body>

Any ideas, thoughts would be most appreciated.

Chris

user1741348
  • 193
  • 3
  • 15

2 Answers2

1

You can detect a touch device with feature detection and then adapt your behavior accordingly with a time-delayed fadeOut():

$(document).ready(function() {

    function hasTouch() {
        try {
           document.createEvent("TouchEvent");
           return(true);
        } catch (e) {
           return(false);
        }    
    }
    var touchPresent = hasTouch();

    //On mouse over those thumbnail
    $('.item-caption').hover(function() {

        //Display the caption
        var caption = $(this).find('.caption');
        caption.stop(true, true).fadeIn(200);
        // on touch systems, fade out after a time delay
        if (touchPresent) {
            caption.delay(5000).fadeOut(600);
        }
    }, function() {    

        //Hide the caption
        $(this).find('.caption').stop(true, true).fadeOut(600);
    });

});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You can use the navigator.userAgent.match to detect mobile devices as follows:

onMobile = false;
// Mobile device detect - terrible, yes, but whatever
if( navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Blackberry/i)
){
onMobile = true;                   
}

Now in your document.ready or wherever you want - Check for if onMobile is true and if it is then do your thing.

Roshan Gautam
  • 4,780
  • 1
  • 13
  • 11
  • User agent detection is a horrible idea - feature detection is nearly ALWAYS a better way to go. If you want to detect touch behavior, you can just use feature detection on Touch. See http://stackoverflow.com/questions/4643443/how-do-i-detect-whether-a-browser-supports-mouseover-events for info. – jfriend00 Nov 16 '12 at 19:21