Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

javascript - Make a div scroll when i reach a certain point

I want to make a div (button) scroll when I reach a certain scrolling point.Also i want to add classes to the other elements on the page.Somehow it isnt working.Here is the code Im using for the scroll,but I dont quite understand it.Please help me

$(window).load(function () {

    var nav = get_nav();

    if (!(nav.is_msie && nav.ver <= 6)) {
        var tmp1 = $('#tag_container').css('position');
        var cur_w_offset = start_offset = $('#butoni').offset().top;
        var bp = $('#right_content');
        var pt = $('#butoni');
        cur_w_offset = $(window).scrollTop();
        if (cur_w_offset >= start_offset) {
            bp.addClass('flyingbuy');
        } else {
            pt.css('top', 'auto');
        }
        if (window.content_center != true || window.c != false) {
            window.buydeal_forpresent = false;
        }
        $(window).scroll(function () {
            var tmp = $('#tag_container').css('position');
            ur_w_offset = $(window).scrollTop();
            if (cur_w_offset >= start_offset) {
                bp.addClass('flyingbuy');
                $('#tag_container').css('height', 'auto');
                $('#tag_container_bialo').slideDown('slow');
            } else {
                bp.removeClass('flyingbuy');
                $('#tag_container').css(({
                    position: "fixed",
                    height: "101px"
                }));
                $('#tag_container_bialo').slideUp('slow');
            }
            if (!window.buydeal_forpresent) {
                var nvp_panel = $('#right_content');
                var nvp_pricetag = $('#butoni');
                var bialo_height = $('#tag_container_bialo').height() + 20;
                var offset_top = nvp_pricetag.offset().top - nvp_panel.offset().top + 16;
                if (tmp == 'fixed') {
                    $('#gift').css('position', 'fixed');
                    var left = $('#butoni').offset().left + 223 - 18;
                    $('#gift').css('top', 16 + bialo_height + 'px');
                    $('#gift').css('left', left + 'px');
                } else {
                    $('#gift').css('position', 'absolute');
                    $('#gift').css('top', offset_top);
                    $('#gift').css('left', '205px');
                    $('#gift').animate({
                        top: offset_top + bialo_height
                    }, 400);
                }
            }
        });
    }
});

I have a problem now.I've been working here : JsFiddle

I don't want the text div to scroll that way.When the pages reaches at the button's level ,I want the txt to show,sliding down and moving along with the button.But,the text needs to be BEFORE the button.I don't know why it grows when sliding down.The sliding up part is ok.


Ok,i found out the growing part.I didnt specify the height. I really dont know how can I make the txt appear before the button and slide together.Like this:

TEXT BUTTON

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not 100% clear on what you're trying to achieve, and there seems to be a lot of code in your post that doesn't directly relate to your question. Here's a much simpler piece of code that seems to do what you want (if I'm interpreting your goal correctly):

JSFiddle Example

$(function(){
    var btn = $('.button');
    var btnPosTop = btn.offset().top;
    var win = $(window);
    win.scroll(function(e){
        var scrollTop = win.scrollTop();
        if(scrollTop >= btnPosTop){
            //we've reached the button
            btn.css({position:'fixed',top:0,marginTop:0});
        }else if(btn.css('position') === 'fixed'){
            //if we scroll back up past the button's original position, and the button had previously been changed to its fixed position, we change it back
            btn.css({position:'',top:'',marginTop:'100px'});
        }
    });
});

When the page loads, we get the original position, relative to the document, of the button's top edge. We then bind the window's scroll event to code that checks how far down the user has scrolled (win.scrollTop()). It compares that value to the button's original position. If it's found to be greater than or equal to the button's original position, that means the button should start scrolling with us by changing its CSS position to fixed. The else if part returns the button to its original position when the user scrolls back up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...