-1

I'm using jQuery to horizontally center a div element that doesn't have a default width. I'm using the following code, but it's not working. if anyone could help me to understand why, it'd be very helpful. PS, I the height has a default height of 20, so that's where the -10px comes from.

var w = ($(".notec").width() + 51) / 2;
$(".buttons").css("margin", "-10px 0 0 -" + w + "px");
4

2 回答 2

0

Centering with respect to .notec. Width is not needed if the element is displaying as block element.

var h = ($(".notec").height() - $(".buttons").height()) / 2;
$(".buttons").css("margin", h+"px auto 0 auto');
于 2012-04-29T15:48:28.960 回答
0

看看:UI/API/1.8/Position http://bit.ly/IvUhLF

$(".overlay").hide();
$(".photo_hold").hover(
  function () {
      $(".overlay", $(this)).show();
      $(".buttons", $(this)).position({
          my: "center",
          at: "center",
          of: $(this)
      });
   },
  function () {
      $(".overlay", $(this)).hide();
  }
);

玩弄变量,让它做你需要的

于 2012-04-29T15:52:01.663 回答