Alistair Gillespie
Alistair Gillespie

Reputation: 550

Change href when anchor is clicked

I have a question about how I can dynamically change a href="" in a button.

The jsfiddle below shows a button fixed at the bottom of the viewport starting at the landing page:

http://jsfiddle.net/Hm6mA/3/

The html of the button is like so:

<div class="button">
    <a href="#first" class="" style="width: 80px; height: 80px; opacity: 1;">
        <img src="img/down.png" alt="down">
    </a>
</div> 

When it is clicked I want it to scroll to the next section and change the href="" to the following section of the page. So, when it is first clicked, the href will change to #second. It would obviously also need to change when the user manually scrolls past a section.

This is for a single page website. How would I go about such a thing?

Upvotes: 0

Views: 252

Answers (4)

Alvaro
Alvaro

Reputation: 41595

You can use fullPage.js plugin to achieve what you want. Maybe it is faster than coding it from cero :)

Upvotes: 1

Ferdinand Torggler
Ferdinand Torggler

Reputation: 1422

I have written a small jQuery plugin for that, just pushed it to GitHub. https://github.com/ferdinandtorggler/scrollstack

What you basically want to do is calling

$('.button').scrollstack({stack: ['#first', '#second', ... ]});

You dont even need the link when you call it on the button. So check it out and let me know if it works for you. ;)

Here you can try it and read more: http://ferdinandtorggler.github.io/scrollstack/

Upvotes: 1

Arun Aravind
Arun Aravind

Reputation: 1318

I am not used to jquery. Here is a pure javascript solution. It surely changes the hash value.

<body>

    <div id="sections">

        <section id="s100">asdfasd</section>
        <section id="s101"></section>
        <section id="s102"></section>
        <section id="s103"></section>
        <section id="s104">asdfasdasdfsdf</section>
        <section id="s105"></section>

    </div>

    <div class="nav-bar">

        <a id="next-button" class="button" href="#s100">Next</a>

    </div>

    <script type="text/javascript">

        var sections = document.getElementById("sections");

        var nextButton = document.getElementById('next-button');

        sections.onscroll = function (evt) {

        }


        var counter = 100;
        var limit = 105;

        // closure
        nextButton.onmouseup = function (evt) {

            var incCounter = function () {
                // add your custom conditions here

                if(counter <= limit)
                    return counter++;
                return 0;
            };

            var c = incCounter();
            if(c != 0)
                this.setAttribute('href', "#s" + c);
        }

    </script>

</body>

CSS

html, body {
            height: 100%;
            width: 100%;
            overflow: hidden;
        }

        #sections {
            height: 50%;
            width: 100%;
            overflow: scroll;
        }

        .nav-bar {
            margin: 30px 20px;
        }

        .button {
            text-decoration: none;
            border: 1px solid #999;
            padding: 10px;
            font-size: 120%;
        }

Upvotes: 1

Satpal
Satpal

Reputation: 133403

Use .prop() to change its value

$(".button").on('click', function(){
    $('.button').find('a').prop('href', '#services');
});

Demo

Upvotes: 2

Related Questions