0

I've been having the hardest time getting an element to be pinned to the left side of the page but also be able to scroll up and down.

I have already tried the method where you just change the left position to $(window).scrollLeft(), but this gives a very choppy-looking animation.

What i'm looking for is the opposite of this fiddle, but I can't quite get it to work:

http://jsfiddle.net/F7Bme/

var leftInit = $(".scroll_fixed").offset().left;
var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-       top').replace(/auto/, 0));

$(window).scroll(function(event) {
   var x = 0 - $(this).scrollLeft();
   var y = $(this).scrollTop();

// whether that's below the form
if (y >= top) {
    // if so, ad the fixed class
    $('.scroll_fixed').addClass('fixed');
} else {
    // otherwise remove it
    $('.scroll_fixed').removeClass('fixed');
}

$(".scroll_fixed").offset({
    left: x + leftInit
   });
});

Notice that this div is fixed top but not left. I'm looking for the opposite, fixed left and not top. Any ideas?

I tried editing this fiddle but can't get it working.

4

2 回答 2

1

这炒锅:

var leftInit = $(".scroll_fixed").offset().left;
var top = $('.scroll_fixed').offset().top - parseFloat($('.scroll_fixed').css('margin-top').replace(/auto/, 0));

$(window).scroll(function(event) {
  var x = $(this).scrollLeft();
  var y = $(this).scrollTop();

  // whether that's below the form
  if (x >= leftInit) {
      // if so, ad the fixed class
      $('.scroll_fixed').addClass('fixed');
  } else {
      // otherwise remove it
      $('.scroll_fixed').removeClass('fixed');
  }

  $(".scroll_fixed").offset({
      left: x + leftInit
  });
});

编辑也改变

.scroll_fixed.fixed {
  position:absolute;
于 2013-05-03T21:26:07.660 回答
1

也许这就是你想要的http://jsfiddle.net/F7Bme/958/

.scroll_fixed {
    position: relative;
    float: left;
    left: 0;
}
.scroll_fixed div {
    float: left;
    position: relative
} 

忘记了JS

于 2013-05-03T21:35:18.830 回答