简体   繁体   中英

Move a div horizontally and then vertically

I made an exemple here of what I have : http://jsfiddle.net/jewb/tM5h7/4/

my script look like this :

var minHeight = $(window).scrollTop();
var maxHeight = $(window).height();
var middleHeight = (maxHeight + minHeight) / 2;

$(window).scroll(function () {
    var positionTopDiapo = $('#diaporama').offset().top;
    var hauteurDivDiapo = $('#diaporama').height()/2;
    var positionTop = $('#frame').offset().top;
    var hauteurDiv = $('#frame').height()/2;
    var positionY = positionTop + hauteurDiv - $(window).scrollTop();
    var positionYDiapo = positionTopDiapo + hauteurDiv - $(window).scrollTop();
    var bottomDiapo = (positionTopDiapo - $(window).scrollTop()+ $('#diaporama').height());
    var bottomFrame = (positionTop - $(window).scrollTop()+$('#frame').height());


    var window_width = $(window).width() - $('#frame').width();
    var document_height = $('#diaporama').height() - $('window').height();
    var scroll_position = $(window).scrollTop();
    var object_position_left = window_width * (scroll_position / document_height);
    $('#frame').css({'left': object_position_left });

    if(positionYDiapo<=middleHeight){
        $('#frame').css({position:'fixed', top: positionTop - $(window).scrollTop(), bottom:'auto'});





                if(bottomDiapo<=bottomFrame){
                    $('#frame').css({ 'position': 'absolute' });
                    $('#frame').css({ 'bottom': '0px' });
                    $('#frame').css({ 'top': 'auto' });
                }

     }  else{
        $('#frame').css({position:'absolute', top:'0px', bottom:'auto'});
        $('#frame').css({ 'left': '0px' });
    }

Here's the problem. At first, #frame has a position absolute, top:0px; When #diaporama reach the middle of the window, I need to fix position of #frame . When #frame is fixed, I need it to scroll from left to right. I need to reach the end of #frame at the same time the bottom of #diaporama reach the middle of the page. When it reach the end, #frame will have a position absolute and bottom:0px;

Then, I continue to scroll till the end of the document. If I scroll up, it will do the same reversely.

Here's what I've done so far : jsfiddle

    var scroll_position = $(window).scrollTop(),
        d = $('#diaporama').get(0).scrollHeight,
        c = $('#diaporama').get(0).scrollWidth,

    scrollPercent = (scroll_position / (d+c));
    var position = (scrollPercent * - $('#frame').width())+positionTopDiapo;
    var positionGauche = $('#frame').offset().left;

    var tarif = $('#tarif').offset().top - $(window).scrollTop();

there is some glitch when I give the left position, but overall it's pretty much great. If anyone can improve the code, just tell me. I put this code to help other who will have the same issue. It could be a start for others who have the same problem has mine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM