0

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?

4

4 回答 4

2

使用.prop()更改其值

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

演示

于 2013-10-06T07:29:47.680 回答
1

我不习惯jquery。这是一个纯javascript解决方案。它肯定会改变哈希值。

<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%;
        }
于 2013-10-06T08:05:10.347 回答
1

您可以使用 fullPage.js 插件来实现您想要的。也许它比从cero编码要快:)

于 2013-10-09T10:14:56.573 回答
1

我为此编写了一个小的 jQuery 插件,只是将它推送到了 GitHub。https://github.com/ferdinandtorggler/scrollstack

你基本上想做的是打电话

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

当您在按钮上调用它时,您甚至不需要该链接。所以检查一下,让我知道它是否适合你。;)

在这里您可以尝试并阅读更多信息:http: //ferdinandtorggler.github.io/scrollstack/

于 2013-10-06T08:27:02.817 回答