admin管理员组

文章数量:1389855

I need to make sure that upon clicking a link to view a html file, on an iPhone or Android or any handheld, it doesn't use Fancybox to view it, as it does on a puter.

I've tried ways with @media with no luck.

Is there any code to disable certain bits of javascript depending on what device it is?

Thanks!

I need to make sure that upon clicking a link to view a html file, on an iPhone or Android or any handheld, it doesn't use Fancybox to view it, as it does on a puter.

I've tried ways with @media with no luck.

Is there any code to disable certain bits of javascript depending on what device it is?

Thanks!

Share Improve this question edited Jan 26, 2012 at 10:36 beryllium 29.8k15 gold badges109 silver badges127 bronze badges asked Jan 26, 2012 at 10:34 malicedixmalicedix 1292 gold badges5 silver badges14 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

Andre's solution will work for a specific subset of devices, but a better approach might be to do it based on screen size, since that's presumably why you don't want to use facnybox (because the screen is too small).

How about this:

if (window.innerWidth < 500 && window.innerHeight < 500) {

    //small screen device - don't use fancy box
}

You can swap out the width and height for whatever the threshold is for fancybox looking okay.

I am using this approach and it is working fine for me...

if( $(window).width() > 480 && (!navigator.userAgent.match(/Android/i) ||
!navigator.userAgent.match(/webOS/i) ||
!navigator.userAgent.match(/iPhone/i) ||
!navigator.userAgent.match(/iPod/i) ||
navigator.userAgent.match(/BlackBerry/))
){
  $(".royalSlide").click().fancybox({
     'overlayShow'  : false,
     'transitionIn' : 'elastic',
     'transitionOut'    : 'elastic'
  });

}

For fancybox 2 this worked for me.

$(".fancybox-img").click( function( e ) {
    if ( window.innerWidth < 799 ) {
        e.stopPropagation();
        e.preventDefault();
    }
})
$(".fancybox-img").fancybox( {
    margin     : 100,
    autoSize   : true,
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

you can use something like

if( navigator.userAgent.match(/Android/i) ||
 navigator.userAgent.match(/webOS/i) ||
 navigator.userAgent.match(/iPhone/i) ||
 navigator.userAgent.match(/iPod/i) ||
 navigator.userAgent.match(/BlackBerry/)
 ){
 // your fancybox instantiation here
}

but it will never be 100% accurate

Thinking inside of the box, no pun intended, a non-js solution:

.fancybox-overlay {
    display: none !important;
}

@media (min-width: 1200px) {
    .fancybox-overlay {
        display: block !important;
    }
};

disable fancybox with screen size

@media (min-width: 1000px) {
.fancybox-is-open {
    display: none !important;
}};

本文标签: javascriptDisable fancybox on handheldsStack Overflow