将一个列为列表的右侧div

时间:2013-11-18 16:25:20

标签: javascript jquery html css scrollto

我想让我的卷轴转到容器中的右侧div,但我尝试的每个答案都失败了。 这是我的清单

<div id="odjContent">
    <div id="1" class="rang0 obj" onclick="aller(0);" style="position:relative;">CCCC</div>
    <div id="4" class="rang0 obj" onclick="aller(0);" style="position:relative;">FFFFFF</div>
    <div id="2" class="rang1 obj" onclick="aller(0);" style="position:relative;">BBDDDDDDB</div>
    <div id="3" class="rang0 obj" onclick="aller(0);" style="position:relative;">EEEEEE</div>
    <div id="6" class="rang0 obj" onclick="aller(0);" style="position:relative;">FFFFFF</div>
    <div id="5" class="rang0 obj" onclick="aller(0);" style="position:relative;">EEEEEE</div>
    <div id="11" class="rang0 obj" onclick="aller(0);" style="position:relative;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam id aliquam leo, quis molestie mauris. Suspendisse id condimentum elit. Sed in quam porttitor, condimentum elit sit amet, condimentum nibh. In volutpat dolor sed mi pharetra, at cursus urna semper. Duis orci purus, porttitor eget metus vel, blandit pellentesque sapien. Nam auctor nunc vel fermentum convallis. Duis ut volutpat mi. Integer commodo euismod justo. Nullam venenatis tristique bibendum. Aliquam adipiscing eget lectus nec molestie. Fusce risus magna, dignissim ut mauris at, vehicula feugiat eros. Sed tincidunt egestas leo vel volutpat. Ut imperdiet, risus ac tempus ultricies, nisl nunc faucibus massa, a interdum dui lacus id magna.</div>

这是我的js:

function gererFocus(id) {
    id = "#" + id;
    console.log("Id: " + id + "  " + $(id).offset().top);
    $('#odjContent').animate({
        scrollTop: $(id).offset().top
    });
}

它不会聚焦在我想要的地方,当我点击两次或使用相同的参数抛出此方法时,我的结果不一样 在

console.log("Id: "+id+"  "+$(id).offset().top);

1 个答案:

答案 0 :(得分:0)

当您以element.id的形式传递整个对象时,您需要执行this,因此您需要使用element在js函数中捕获它,其中element是您点击的div

function gererFocus(element) {
    var id = "#" + element.id; // Change here
    .... rest of code .....
}

希望这会有所帮助!