scrollTop不使用jQuery的示例?

时间:2014-03-03 05:55:39

标签: javascript jquery

你能否告诉我为什么scrollTop无效。实际上我想把焦点放在那里,而我给命令预先添加文本。 实际上,焦点是文本前置文本的顶部。我需要重点应该保持不变。

这是我的小提琴 http://jsfiddle.net/d9584/21/

当我在前面添加文本时,焦点会变为行顶。

$("#contend").scroll(function () {
    if ($(this).scrollTop() === 0) {
        alert("ssss");
        $("#pre").prepend(" Distinguished service of a high degree to Australia or to humanity at largefjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda  sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda  sdflkjsdlfjsldfj sdfsjdflsf adasda  sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf sdfjsflsj sdflkjsdlfjsldfj sdfsjdflsf adasda");
        alert($("#pre")[0].scrollHeight)
        $("#pre").prependTo("#contend");
        document.getElementById("content").scrollTop = $("#pre")[0].scrollHeight
    }
});

$("#contend").scroll(function () {
    if ($('#contend').scrollTop() >= $('#contend')[0].scrollHeight - document.body.offsetHeight) {
        $("#pre").html('');
    }
});

1 个答案:

答案 0 :(得分:0)

我认为

$(this).scrollTop() 

永远不会是0

尝试改变

if ($(this).scrollTop() === 0) {

if( $(this).scrollTop() < 1) {