简体   繁体   English

滚动按钮jQuery无法立即运行

[英]Scrolling button jQuery doesn't work instantly

I'm working on some web pages that use a button to scroll to the next div. 我正在一些使用按钮滚动到下一个div的网页上工作。 I can get it to work on every page, except in this particular instance (see jsfiddle). 我可以使它在每个页面上工作,除了在这个特定实例中(请参阅jsfiddle)。

My problem is that the buttons don't work on loading the page, the user first has to start scrolling manually, before the buttons work. 我的问题是按钮在加载页面时不起作用,用户首先必须手动开始滚动,然后按钮才能工作。 I'm assuming that's because of some fault in my jQuery coding, which I've looked over and over, but I can't seem to find the problem. 我假设这是由于我的jQuery编码中的某些错误,我已经一遍又一遍地查看了,但是我似乎找不到问题。 Is there anyone who is a bit more familiar with jQuery than I am who can offer me a solution? 有谁比我更熟悉jQuery,谁能为我提供解决方案?

http://jsfiddle.net/y5wx7nst/3/ http://jsfiddle.net/y5wx7nst/3/

 $(document).ready(function () {

var currentElement = $("#bodytext > div:nth-child(1)");

var onScroll = function () {
    var container = $("#bodytext");
    var children = $(".section");
    for (var i = 0; i < children.length; i++) {
        var child = $(children[i]);
        var childLeft = container.offset().left < child.offset().left;
        if (childLeft) {
            currentElement = child;
            console.log(currentElement);
            return;
        }
    }
};

var scrollToElement = function ($element) {
    var container = $("#bodytext");
    var children = $(".section");
    var width = 0;
    for (var i = 0; i < children.length; i++) {
        var child = $(children[i]);
        if (child.get(0) == $element.get(0)) {
            if (i === 0) {
                width = 0;
            }
            container.animate({
                scrollLeft: width
            }, 500);
            onScroll();
        }
        if (child.next().length > 0) {
            width += child.next().offset().left - child.offset().left;
        } else {
            width += child.width();
        }
    }
};

var buttonright = function (e) {
        scrollToElement(currentElement.next());
    };

var buttonleft = function (e) {
    var container = $("#bodytext");
    if (currentElement.prev().length > 0) {
        if (container.offset().left == currentElement.prev().offset().left) {
            currentElement = currentElement.prev().prev().length > 0 ? currentElement.prev().prev() : currentElement.prev();
        } else {
            currentElement = currentElement.prev();
        }
    }
    scrollToElement(currentElement);
};

$("#bodytext").scroll(onScroll);
$("#buttonright").click(buttonright);
$("#buttonleft").click(buttonleft);

});

You are only calling the onScroll() function after you initially scroll: 最初滚动后,仅调用onScroll()函数:

$("#bodytext").scroll(onScroll);

I added this before that declaration and it all worked: 我在声明之前添加了它,并且一切正常:

onScroll();

jsfiddle: http://jsfiddle.net/y5wx7nst/5/ jsfiddle: http : //jsfiddle.net/y5wx7nst/5/

When code run value of currentElement is not correct. 当currentElement的代码运行值不正确时。 So you should calculate it calling a function onScroll(); 因此,您应该通过调用onScroll()函数来计算它;

...
$("#bodytext").scroll(onScroll);
$("#buttonright").click(buttonright);
$("#buttonleft").click(buttonleft);
onScroll();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM