Reputation: 75
Let me first say I am a complete newb at jQuery, I am having a hard time fixing my current problem. I'm currently working on a project that uses the jQuery ScrollTo plugin. And I am trying to change the axis of the scrollTo function based on my current position on the webpage.
$(document).ready(function() {
var $current_position = thuis;
var $axis = 'xy';
$('.box').stop().scrollTo(thuis, 0);
$(window).resize(function() {
$('.box').stop().scrollTo($current_position, 0);
});
// Scroll function
$('.toc a').click(function() {
var $target = $(this.hash);
if($current_position == bezoekers_info || liniepad_info){
$axis = 'xy';
alert("xy");
} else {
$axis = 'yx';
alert("yx");
}
$('.box').stop().scrollTo($target, 1000, { queue:true, axis: $axis }, {easing: "easeInOutQuart"});
$current_position = $target;
return false;
});
});
The function basically works, but it will not change the axis value (it will stay at 'xy'). Whatever my position is, it will always alert me with "xy". Yet I know $current_position should work, because it does work in the window.resize function. If I change the $axis var at the top to 'yx' it will also work.
What am I missing here, obviously I am doing something wrong with my if / else statement?
Upvotes: 1
Views: 4156
Reputation: 75
I managed to fix it using this:
var $target = $(this.hash);
if( (this.hash.substr(1) == "bezoekers_info") || (this.hash.substr(1) == "liniepad_info") ){
$axis = 'yx';
} else {
$axis = 'xy'
}
Apparently if you check the real text value of this.hash, you can get it to work. Not sure if it proper coding though, but at least it works for now.
Upvotes: 1
Reputation: 14937
add this to check your code...
alert($current_position + ', ' + bezoekers_info + ', ' + liniepad_info);
if those values are as expected, this should do the trick:
if( ($current_position == bezoekers_info) || ($current_position == liniepad_info) ){...
Upvotes: 1