0

I've updated my question as I think I have a solution to my original problem. I want to convert the $margins to a global variable that I can use to calculate my $offset for another function. I need to update the variable after (arrow) keypresses, mouse clicks and possibly mouse wheel actions. Is there a simple solution to this?

Here is my code:

$(document).keydown(function(e){  //keyboard bind   
    if( e.keyCode == 39 ){//right (->)
        if($('.selectstyle').hasClass("selectstyle")){
            $margins = -180;
        } else ($(".select" ).hasClass("select")){
            $margins = 0;
        }
    }
});
$offset = (-220 + $margins),            

The code I've put up is probably not correct for my solution. The site I'm working on is http://lastnighti.co.uk/ I want users to navigate the site primarily with the arrow keys using the serialscroll plugin. The issue I'm having is that if the user highlights an image by pressing the down arrow, and then proceeds to press the right arrow key to proceed to the next image, it is overshot due to serialscroll taking the margins width in to account.

This has been causing me grief for over a week. I have tried queuing and setTimout functions, but they disrupt the fluidity of the UI. Basically my problem above is to change the $offset variable to account for when and image is highlighted with the .selectstyle class. I'd appreciate any solution to the above problem to dynamically control the $offset.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    you dont convert it, you set a global and then asign it on keydown or whatever – SpYk3HH Apr 22 '12 at 01:39
  • you might want to check the syntax. – Joseph Apr 22 '12 at 01:42
  • Because I'm using the arrow keys for serialScroll and to change the style of elements in a scroll window, I need to use the $offset to compensate for changes in CSS margin changes and to scroll the correct amount of pixels. – user1317417 Apr 22 '12 at 01:45
  • @user1317417. Can't you declare `$margins` in the global scope? – gdoron Apr 22 '12 at 01:50

1 Answers1

0

Making a global variable is being done with window:

window.varName = "foo";

so you want this:

$(document).keydown(function(e){  //keyboard bind   
    if( e.keyCode == 39 ){//right (->)
        if($('.selectstyle').hasClass("selectstyle")){
            window.$margins = -180;
        } else ($(".select" ).hasClass("select")){
            window.$margins = 0;
        }
    }
});
$offset = (-220 + $margins),  

Note that there're probably better ways to do what you want.

Maybe this is o.k. by you:

var $margins;

$(document).keydown(function(e){  //keyboard bind   
    if( e.keyCode == 39 ){//right (->)
        if($('.selectstyle').hasClass("selectstyle")){
            $margins = -180;
        } else ($(".select" ).hasClass("select")){
            var $margins = 0;
        }
    }
});

$offset = (-220 + $margins),  
gdoron
  • 147,333
  • 58
  • 291
  • 367