0

I've already seen this sometimes, but I don't know how to realize it or how to search for it. That's why I would like to explain what it does:

Let's say we got an image, an arrow and it points downwards and when you click on it, the page scrolls down. When I now scroll down and reach a specified point the arrow points upwards and brings me back to the top of the page.

EDIT: I found an example. See the small arrow at the bottom and how it changes if you reach the end of the page. rorymcilroy.com

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
zeekrey
  • 391
  • 6
  • 16

3 Answers3

1

you could use the jQuery ScrollTo Plugin

here is a demo

here is a jsfiddle demo for what you want (using the scrollTo plugin)

jsfiddle demo

the javascript:

$('#upDownArrow').click(function() {
    if($(this).hasClass("arrowUp")) {
        $.scrollTo( '#top', 500); // scroll to top      
        $(this).removeClass("arrowUp");
        $(this).addClass("arrowDown");
    } else {
        $.scrollTo( '#bottom', 500); // scroll to bottom
        $(this).removeClass("arrowDown");
        $(this).addClass("arrowUp");     
    }
});

the html:

<div id="#top"></div>
<div id="upDownArrow" class="arrowDown"><div class="arrow"></div></div>

<!-- add your content here -->

<div id="#bottom"></div>

the css:

#upDownArrow {
    position: fixed;
    top: 50px;
    right: 50px;
    width: 30px;
    height: 30px;
    background: #33B;
}
#upDownArrow:hover {
    background: #99F;
    cursor: pointer;
}

#upDownArrow.arrowDown:hover .arrow {
    border-top-color: #99F;   
}

#upDownArrow.arrowUp:hover .arrow {
    border-bottom-color: #99F;   
}

#upDownArrow.arrowUp .arrow {
    position: absolute;
    border: 15px solid transparent;
    border-bottom: 15px solid #33B;
    top: -30px;
}

#upDownArrow.arrowDown .arrow {
    position: absolute;
    border: 15px solid transparent;
    border-top: 15px solid #33B;
    bottom: -30px;
}

if you want a previous / next button, you might have to create a list of items / id's / positions (look at the documentation of the scrollTo plugin to see what parameters you can use) that should get stepped through like that:

var steps = ["top","part1","part2","bottom"];

and then create 2 buttons, one for up, one for down and then use:

var currentPosition = 0;

$("#buttonUp").click(function() {
    if(currentPosition == 0) {
        currentPosition = steps.length-1;
    } else {
        currentPosition--;
    }
    $.scrollTo('#'+steps[currentPosition],500);        
});

$("#buttonDown").click(function() {
    if(currentPosition == steps.length-1) {
        currentPosition = 0;
    } else {
        currentPosition++;
    }
    $.scrollTo('#'+steps[currentPosition],500);        
});

here is a demo with up and down button:

jsfiddle demo with up and down

jsfiddle demo up and down fullscreen

here another demo with your "button-changes" on scroll ;)

jsfiddle button changes demo

r3bel
  • 1,350
  • 8
  • 12
  • Thanks, but thats not what I'm looking for. Because the button, or the image should be fixed. So that it will never change its position. It will just change the target and the behavior. – zeekrey Aug 22 '12 at 21:08
  • it is what you were looking for, look at the jsfiddle demo ;) – r3bel Aug 22 '12 at 21:33
0

If I understand you correctly, the this question is the same as yours.

Community
  • 1
  • 1
Ivan P
  • 1,920
  • 2
  • 15
  • 19
0

I wrote this a while back. I think it's what you are looking for.

$('a').on('click', function (event) {
    event.preventDefault();//stop the browser from jumping to the anchor
    var href  = $(this).attr('href'),  // get id of target div from link e.g #one #two 
        oset  = $(href).offset().top;   // get the offset top of the div
    $('html, body').stop().animate({
        scrollTop : oset  // animate the scrollTop property to the offset of target div
    }, 1000, function () {   
        location.hash = href; // change the hash to target div  
    });
});
aziz punjani
  • 25,586
  • 9
  • 47
  • 56
  • Thank you =) But this is not connected to the current view. But I found a example. See the small arrow at the bottom and how it changes if you reach the end of the page. http://rorymcilroy.com/ – zeekrey Aug 22 '12 at 21:19