星期二, 3月 15, 2011

js取得置中位置

  1. 左右皆置中
    var $dialog = $("<div>hello</div>");
    var left = ($(window).width() - $dialog.outerWidth()) / 2 + $(window).scrollLeft() + "px";
    var top = ($(window).height() - $dialog.outerHeight() ) / 2 + $(window).scrollTop() + "px";
  2. click button後,置於button正下方
    onclick = "showDialog(this)
    function showDialog(obj)
    var margin_top = 30; //自行預設設margin top距離

    var pos = $(obj).offset();
    var eWidth = $(obj).outerWidth();
    var mWidth = $confirmBox.outerWidth();
    var left = (pos.left - mWidth/2) + "px";
    var top = margin_top + pos.top + "px";

沒有留言: