te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Add class to an element when user scrolls to 50px above it? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add class to an element when user scrolls to 50px above it? - Stack Overflow

programmeradmin3浏览0评论

Does anyone know how I can achieve the following with jQuery:

I want to add a class (.fixed) to an element (#content) when a user reaches 50px above #content. And then, when the user scrolls up 50px above #content, I want to remove the class.

How can I do this with as little script as possible?

<div id="header">
</div>

<div id="content">
</div>

<div id="content-2">
</div>

FIDDLE

Does anyone know how I can achieve the following with jQuery:

I want to add a class (.fixed) to an element (#content) when a user reaches 50px above #content. And then, when the user scrolls up 50px above #content, I want to remove the class.

How can I do this with as little script as possible?

<div id="header">
</div>

<div id="content">
</div>

<div id="content-2">
</div>

FIDDLE

Share Improve this question edited Mar 5, 2015 at 3:35 caustic asked Mar 5, 2015 at 3:27 causticcaustic 5852 gold badges8 silver badges18 bronze badges 1
  • can you clarify? you want to add a class when they are 50px above content, then remove the class when they are...50 px above content? I don't quite undertand – chiliNUT Commented Mar 5, 2015 at 3:53
Add a ment  | 

3 Answers 3

Reset to default 7

If I understand correctly, this should do the trick.

$(function(){
    $(document).scroll(function(){
        if($(this).scrollTop() >= $('#content').offset().top - 50) {
            $("#content").css("background","red");
        } else {
            $("#content").css("background","orange");
        }
    });
});

Basically, it check the current position of the user's scroll and pare it to the position of the div minus 50 pixel.

If you just past this code in your document, it should work properly.

Try this,

$(window).scroll(function() {

    if ($(this).scrollTop() > 50){  
        $('#content').addClass("content_fixed");
    }
    else{
        $('#content').removeClass("content_fixed");
    }
});

Demo : http://jsfiddle/UI_Designer/8j0a1Lkk/1/

You can use the jquery plugin waypoint (http://imakewebthings./waypoints/) to detect when the user has scrolled to the area and then use the javascript .innerhtml function to change the html code. The one problem with this method is that you have to have another element that surrounds your main element ONLY.

发布评论

评论列表(0)

  1. 暂无评论