1

如果我有这样的代码,滑块将不会滑动:

var $root = $('html, body');
$('.main-menu a').each(function () {
    // capture the offset on init not click
    var that = $(this),
        offset = $(that.attr('href')).offset().top - $('nav.main-menu').height() + 1;
    // then use that offset, so we are not querying for id on each click
    that.click(function () {
        $root.animate({
            scrollTop: offset
        }, 2000);
    });
});

$('.cycle-container').each(function () {
    $(this).cycle({
        fx: 'scrollLeft',
        speed: 'slow',
        timeout: 0,
        next: this
    });
});

如果我将其更改为:它可以工作。:秒

$('.cycle-container').each(function () {
    $(this).cycle({
        fx: 'scrollLeft',
        speed: 'slow',
        timeout: 0,
        next: this
    });
});

var $root = $('html, body');
$('.main-menu a').each(function () {
    // capture the offset on init not click
    var that = $(this),
        offset = $(that.attr('href')).offset().top - $('nav.main-menu').height() + 1;
    // then use that offset, so we are not querying for id on each click
    that.click(function () {
        $root.animate({
            scrollTop: offset
        }, 2000);
    });
});

一般来说,我是 js 的新手。我刚刚换了顺序。关于为什么这个订单会使代码工作而第一个订单不能工作的任何理由或想法?

不起作用,我的意思是,我单击滑块但它不滑动。

我去firebug,点击console,然后点击ALL,我看到:

TypeError: d.detachEvent is not a function
[Break On This Error]   

if ( event.target.nodeType === 3 ) {

"CSS Usage: initializing extensions" TypeError: $(...).offset(...) is undefined [Break On This Error]   

return event.result;

其中第一个,是 jquery1.9.1 自己返回的,但我没有碰它。

html结构:

<div class="cycle-container">
    <div id="row-slide2">
        <section id="slide2">
             <h1>bla bla</h1>

            <p>ble ble ble</p>
        </section>
    </div>
    <div id="row-slide2-1">
        <section id="slide2-1">
             <h1>bli bli</h1>

            <div class="table">
                <div class="column">
                     <h2>blo blo</h2>

                    <p>blu blu</p>
                </div>
            </div>
        </section>
    </div>
</div>

jQuery 1.9.1;

航点插件;

循环插件;

更新 如果我将代码更改为:

var that   = $(this);
var offset = $(that.attr('href')).offset().top-$('nav.main-menu').height()+1;

代替:

var that   = $(this),
    offset = $(that.attr('href')).offset().top-$('nav.main-menu').height()+1;

实际上,无论哪种顺序都可以使用。:S

唯一的问题是,代码现在会出现不希望的闪烁。而且,此错误仍然出现在控制台上:

TypeError: $(...).offset(...) is undefined
[Break On This Error]   

var offset = $(that.attr('href')).offset().top-$('nav.main-menu').height()+1;

请相应地检查 jsfiddle:http: //jsfiddle.net/talofo/Hq9NL/2/

关于这个小提琴的说明: 小提琴或多或少代表了我的本地代码。但是,从一开始,本地代码顺序就很重要,在小提琴上它无关紧要,它总是有效的。

4

1 回答 1

1

I end up dropping the all offset thing:

I just did:

var $root = $('html, body');

$('.main-menu a').click(function() {
 $root.animate({
  scrollTop: $($(this).attr('href')).offset().top-$('nav.main-menu').height()+1
 }, 2000
);

return false;
});

And changed the html structure. So, now the offset is set onclick.

And the waypoint call I changed to:

$('#container>div').each(function()

Thanks for your time towards this. And sorry for being so specific. I will preserve this, because, for some reason someone may find it useful and, with me, notice that the issue of order may rely on console errors, or bad html structure.

于 2013-07-29T09:20:26.110 回答