国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

javascript與CSS復習(《精通javascript》)

2024-09-06 12:45:33
字體:
來源:轉載
供稿:網友
如:elem.style.height 或者 elem.style.height = '100px', 這里要注意的是設置任何幾何屬性必須明確尺寸單位(如px),同時任何幾何屬性返回的是表示樣式的字符串而非數值(如'100px'而非100)。另外像elem.style.height這樣的操作,也能獲取元素style屬性中設置的樣式值,如果你把樣式統一放在css文件中,上述方法只會返回一個空串。為了獲取元素真實、最終的樣式,書中給出了一個函數
代碼如下:
//get a style property (name) of a specific element (elem)
function getStyle(elem, name) {
  // if the property exists in style[], then it's been set
  //recently (and is current)
if(elem.style[name]) return elem.style[name];
//otherwise, try to use IE's method
else if (elem.currentStyle) return elem.currentStyle[name];
//Or the W3C's method, if it exists
else if (document.defaultView && document.defaultView.getComputedStyle) {
      //it uses the traditional 'text-align' style of rule writing
      //instead of textAlign
name = name.replace(/[A-Z]/g, '-$1');
name = name.toLowerCase();
//get the style object and get the value of the property ( if it exists)
      var s = document.defaultView.getComputedStyle(elem,'');
return s && s.getPropertyValue(name);
  } else return null;
}

理解如何獲取元素的在頁面的位置是構造交互效果的關鍵。先復習下css中position屬性值的特點。
static:靜態定位,這是元素定位的默認方式,它簡單的遵循文檔流。但元素靜態定位時,top和left屬性無效。
relative:相對定位,元素會繼續遵循文檔流,除非受到其他指令的影響。top和left屬性的設置會引起元素相對于它的原始位置進行偏移。
absolute:絕對定位,絕對定位的元素完全擺脫文檔流,它會相對于它的第一個非靜態定位的祖先元素而展示,如果沒有這樣的祖先元素,它的定位將相對于整個文檔。
fixed:固定定位把元素相對于瀏覽器窗口而定位。它完全忽略瀏覽器滾動條的拖動。
作者封裝了一個跨瀏覽器的獲取元素頁面位置的函數
其中有幾個重要元素的屬性:offsetParent,offsetLeft,offsetTop(可直接點擊到Mozilla Developer Center的相關頁面)
代碼如下:
//find the x (horizontal, Left) position of an element
function pageX(elem) {
  //see if we're at the root element, or not
return elem.offsetParent ?
//if we can still go up, add the current offset and recurse upwards
      elem.offsetLeft + pageX(elem.offsetParent) :
      //otherwise, just get the current offset
      elem.offsetLeft;
}
//find the y (vertical, top) position of an element
function pageY(elem) {
  //see if we're at the root element, or not
  return elem.offsetParent ?
//if we can still go up, add the current offset and recurse upwards
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蚌埠市| 札达县| 敖汉旗| 福清市| 罗源县| 莱州市| 昂仁县| 汕头市| 于田县| 阳曲县| 唐海县| 新营市| 扶余县| 永春县| 乌拉特后旗| 紫云| 大厂| 孟村| 昌黎县| 六枝特区| 贡觉县| 金塔县| 班戈县| 咸丰县| 彝良县| 资中县| 浪卡子县| 牡丹江市| 阳谷县| 呼玛县| 剑河县| 洪泽县| 广水市| 和龙市| 武穴市| 蒙阴县| 东明县| 孝感市| 栾城县| 额尔古纳市| 邛崃市|