-2

Olá pessoal queria que essa minha função scroll fosse desabilitada para mobiles, ou seja só estará ativa se a largura da tela for 1200px pra cima.

$(window).scroll(function () {

// Movimento primeiro box vermelho
if ($(this).scrollTop() >= $("#box_img1").offset().top - $("#marcador_menu").offset().top) {
   $("#box_img1").stop().animate({"margin-right":"70px", "opacity":"1"},600);
   $("#box_text1").stop().animate({"margin-left":"100px", "opacity":"1"},600);
} else {
}
if ($(this).scrollTop() >= $("#box_img2").offset().top - $("#marcador_menu").offset().top) {
   $("#box_img2").stop().animate({"margin-left":"100px", "opacity":"1"},600);
   $("#box_text2").stop().animate({"margin-right":"-50px", "opacity":"1"},600);
} else {
}
if ($(this).scrollTop() >= $("#box_img3").offset().top - $("#marcador_menu").offset().top) {
   $("#box_img3").stop().animate({"margin-right":"50px", "opacity":"1"},600);
   $("#box_text3").stop().animate({"margin-left":"100px", "opacity":"1"},600);
} else {
}
if ($(this).scrollTop() >= $("#box_img4").offset().top - $("#marcador_menu").offset().top) {
   $("#box_img4").stop().animate({"margin-left":"100px", "opacity":"1"},600);
   $("#box_text4").stop().animate({"margin-right":"-50px", "opacity":"1"},600);
} else {
}
if ($(this).scrollTop() >= $("#box_img5").offset().top - $("#marcador_menu").offset().top) {
   $("#box_img5").stop().animate({"margin-right":"50px", "opacity":"1"},600);
   $("#box_text5").stop().animate({"margin-left":"100px", "opacity":"1"},600);
} else {
}
});
1

3 Respostas 3

2

SOLUÇÃO

$(window).scroll(function(){
    if ($(window).width() >= 1200){ 
        // do something here
    }   
});
2

Insira seu código dentro do seguinte if:

if (window.innerWidth >= 1200) {
    //Seu code aqui =]
}

A mesma funcionalidade acima, porém dessa vez em Jquery juntamente ao seu código:

if ($(window).width() >= 1200) {
 $(window).scroll(function () {

  // Movimento primeiro box vermelho
  if ($(this).scrollTop() >= $("#box_img1").offset().top - $("#marcador_menu").offset().top) {
     $("#box_img1").stop().animate({"margin-right":"70px", "opacity":"1"},600);
     $("#box_text1").stop().animate({"margin-left":"100px", "opacity":"1"},600);
  } else}
  if ($(this).scrollTop() >= $("#box_img2").offset().top - $("#marcador_menu").offset().top) {
     $("#box_img2").stop().animate({"margin-left":"100px", "opacity":"1"},600);
     $("#box_text2").stop().animate({"margin-right":"-50px", "opacity":"1"},600);
  } else {}
  if ($(this).scrollTop() >= $("#box_img3").offset().top - $("#marcador_menu").offset().top) {
     $("#box_img3").stop().animate({"margin-right":"50px", "opacity":"1"},600);
     $("#box_text3").stop().animate({"margin-left":"100px", "opacity":"1"},600);
  } else {}
  if ($(this).scrollTop() >= $("#box_img4").offset().top - $("#marcador_menu").offset().top) {
     $("#box_img4").stop().animate({"margin-left":"100px", "opacity":"1"},600);
     $("#box_text4").stop().animate({"margin-right":"-50px", "opacity":"1"},600);
  } else {}
  if ($(this).scrollTop() >= $("#box_img5").offset().top - $("#marcador_menu").offset().top) {
     $("#box_img5").stop().animate({"margin-right":"50px", "opacity":"1"},600);
   $("#box_text5").stop().animate({"margin-left":"100px", "opacity":"1"},600);
  } else {}
 });
}

Como informação adicional um outro if interessante para mobile é comparando se existe touch no dispositivo:

if(('ontouchstart' in window)){
   //O code aqui sera executado apenas em dispositivos que possuem Touch Screen
}
1

Você poderia fazer da seguinte forma:

$(window).resize(function() {

  if ($(window).width() > 1200) {
    $(window).scroll(myFunction);
  } else {
    $(window).unbind('scroll');
  }

});

var myFunction = function() {

  // Movimento primeiro box vermelho
  if ($(this).scrollTop() >= $("#box_img1").offset().top - $("#marcador_menu").offset().top) {
    $("#box_img1").stop().animate({"margin-right":"70px", "opacity":"1"},600);
    $("#box_text1").stop().animate({"margin-left":"100px", "opacity":"1"},600);
  } else {
  }
  if ($(this).scrollTop() >= $("#box_img2").offset().top - $("#marcador_menu").offset().top) {
    $("#box_img2").stop().animate({"margin-left":"100px", "opacity":"1"},600);
    $("#box_text2").stop().animate({"margin-right":"-50px", "opacity":"1"},600);
  } else {
  }
  if ($(this).scrollTop() >= $("#box_img3").offset().top - $("#marcador_menu").offset().top) {
    $("#box_img3").stop().animate({"margin-right":"50px", "opacity":"1"},600);
    $("#box_text3").stop().animate({"margin-left":"100px", "opacity":"1"},600);
  } else {
  }
  if ($(this).scrollTop() >= $("#box_img4").offset().top - $("#marcador_menu").offset().top) {
    $("#box_img4").stop().animate({"margin-left":"100px", "opacity":"1"},600);
    $("#box_text4").stop().animate({"margin-right":"-50px", "opacity":"1"},600);
  } else {
  }
  if ($(this).scrollTop() >= $("#box_img5").offset().top - $("#marcador_menu").offset().top) {
    $("#box_img5").stop().animate({"margin-right":"50px", "opacity":"1"},600);
    $("#box_text5").stop().animate({"margin-left":"100px", "opacity":"1"},600);
  } else {
  }

});

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .