1

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?

3
  • Are you trying to say this in your if statement? $current_position == bezoekers_info || $current_position == liniepad_infootherwise it will always be true if liniepad_info is set. Commented Aug 1, 2013 at 19:11
  • I am actually. Basically, I want it to say: If current position is either bezoekers_info or liniepad_info, return xy. If it is any of the other 3 possibilities, return yx. I tried it with what you said, but now it constantly returns yx. Commented Aug 1, 2013 at 19:23
  • They way it is now, the if block will always run. Commented Aug 1, 2013 at 19:44

2 Answers 2

1

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) ){...
Sign up to request clarification or add additional context in comments.

1 Comment

Still not working, the alert returns HTMLDivElement, while the new if statement always returns yx.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.