scrollTo()不会在标签后滚动('select',#)

时间:2011-12-14 08:57:03

标签: jquery jquery-plugins

我正在失去理智,这应该是简单的,我确信我会像jQuery noob那样过度复杂化。叹。

所以我有这个页面需要#switchto = ac#goto = acemergency hashtags并适当地切换jQuery标签,然后它应该转到(滚动到)所需的ID。问题是tabs()会自动滚动回容器的顶部,当调用scrollTo()时,它不再知道滚动的位置。换句话说,它根本不滚动。

这是我的事件处理程序:

jQuery(window).hashchange(function(e) {
    if (window.location.hash) {
        var hash = window.location.hash.substring(1); //Puts hash in variable, and removes the # character
        var instruct = hash.split('#'); //split instructions
        var whichtab = instruct[0].split('='); // split instruction from tab name

        try {
            var whichid = instruct[1].split('='); // split instruction from id name
        }
        catch (err) { /*suppress*/
        }

        if (whichtab[0] == "switchto") {
            switch (whichtab[1]) {
                case 'ac':
                    try {
                        if (whichid[1] != "") {
                            $s.when(targettab(0)).then(targetid(whichid[1]));
                        }
                    } catch (err) {}
                    $s('.tour').tabs('select', 0);
                    break;
                case 'heating':
                    try {
                        if (whichid[1] != "") {
                            $s.when(targettab(1)).then(targetid(whichid[1]));
                        }
                    } catch (err) {}
                    $s('.tour').tabs('select', 1);
                    break;
                case 'plumbing':
                    try {
                        if (whichid[1] != "") {
                            $s.when(targettab(2)).then(targetid(whichid[1]));
                        }
                    } catch (err) {}
                    $s('.tour').tabs('select', 2);
                    break;
            }
        }
    }
});

这是我的职能:

function targetid(givenid) {
    //$s('html,body').animate({scrollTop: $s("#"+givenid).offset().top},'fast');
    $s.scrollTo($s("#" + givenid), 400);
    $s("#" + givenid).delay(500).fadeTo('slow', 0.1, function() {
        $s("#" + givenid).fadeTo('slow', 1);
        console.log('got ' + givenid + ' and scrolled to it');
    });
}

function targettab(giventab) {
    $s('.tour').tabs('select', giventab);
}

我尝试了几种在stackoverflow上找到的方法,但似乎没有一种方法能解决我的问题。有任何想法吗?有没有办法阻止tabs()在tabsselect上滚动回到顶部?

1 个答案:

答案 0 :(得分:0)

我对你的代码并没有那么认真,但你不是真的是指scrollTop而不是scrollTo吗?假设$ s是你的jQuery对象,当你尝试时会发生什么:

$s("#" + givenid).scrollTop(400);

这也假设$ s(“#”+ givenid)是要滚动的容器的正确选择器。

我可能会离开这里(再次,看起来并不那么难),但我希望它有所帮助!

相关问题