Creating unique browser actions(Jquery)

Asked

Viewed 115 times

0

Good afternoon!

I wonder if it is possible to create a Function with Jquery exclusive for a specific browser

For example:

    $(document).ready(function(e) {
     $("#imagens a").click(function(e) {
        var id = $(this).prop("id");
            id = "Imagens/foto"+id+".jpg";

         $(window).bind('scroll', setTopo);

         $("#img_principal").css('display','block');

         $("#img_principal").attr("src",id);    
    });

    $(".titulo").click(function(e) {
        $("#img_principal").css('display','none');
         $(window).unbind('scroll', setTopo);
    });  
});

Where my faith is:

   function setTopo(){
      $(window).scrollTop(2000);
    }

I wanted to open in Firefox/Opera the values(scrollTop) were other but with the same function, since the structure I built has a difference when running in these browsers. I used Chrome as a base and from there I started to adapt in other browsers.

Grateful

  • Yes this is possible, it would be interesting to explain the problem and what is the behavior/ technology that gives different results in different browsers and so we help normalize the values.

  • When opening an image I wanted the mouse scroll to be locked, so I used scroll, function with value(px). The problem is that when opening in another browser the value has variation, in that the image in question appears cropped or out of position (since the browser hangs at the value I reported).

1 answer

0

You must use this code to identify the user’s browser:

if (jQuery.browser.mozilla){
        // Seu código aqui se for o firefox seu navegador
    else if (jQuery.browser.msie){
        // Seu código aqui se for o Internet Explorer
    else if (jQuery.browser.safari){
        // Seu código aqui se for o Safari
    else if (jQuery.browser.opera){
        // Seu código aqui se for Opera
    } else {
        // Seu código aqui
});

For your example:

function setTopo(){
    if (jQuery.browser.mozilla){
        // Seu código aqui se for o firefox seu navegador
        $(window).scrollTop(2000);
   else if (jQuery.browser.msie){
        // Seu código aqui se for o Internet Explorer
        $(window).scrollTop(3000);
    else if (jQuery.browser.safari){
        // Seu código aqui se for o Safari
        $(window).scrollTop(4000);
    else if (jQuery.browser.opera){
        // Seu código aqui se for Opera
        $(window).scrollTop(5000);
    else {
        // Seu código aqui
        $(window).scrollTop(6000);
    })

}
  • Remembering only that it was removed in version 1.9 jQuery.

  • I put the whole function? I found something similar when I researched, but I couldn’t apply it. Inside each if Else goes the command I want to apply in the browser?

  • @Randrade - In fact I’m using 1.12, there is some alternative or I should use the older version?

  • @A.Lfreire, I edited the question, check the answer to see if it’s what you want

  • @Daviddamasceno I believe that’s it, I’ll test it here

  • @A.Lfreire You can use the jQuery Migrate or the navigator.userAgent

  • @Randrade I was informing myself about Migrate, have you used it? Just link migrate.js on the site and the old commands work?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.