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

首頁 > 開發 > CSS > 正文

Css利用js的expression實現的效果

2020-10-23 18:43:38
字體:
來源:轉載
供稿:網友
IE5及其以后版本支持在CSS中使用expression,用來把CSS屬性和Javas cript表達式關聯起來,這里的CSS屬性可以是元素固有的屬性,也可以是自定義屬性。就是說CSS屬性后面可以是一段Javas cript表達式,CSS屬性的值等于Javas cript表達式計算的結果。 在表達式中可以直接引用元素自身的屬性和方法,也可以使用其他瀏覽器對象。這個表達式就好像是在這個元素的一個成員函數中一樣。 

  給元素固有屬性賦值

  例如,你可以依照瀏覽器的大小來安置一個元素的位置。

#myDiv {
position: absolute;
width: 100px;
height: 100px;
left: expression(document.body.offsetWidth - 110 + "px");
top: expression(document.body.offsetHeight - 110 + "px");
background: red;
}

  給元素自定義屬性賦值

  例如,消除頁面上的鏈接虛線框。 通常的做法是:

<a href="link1.htm" onfocus="this.blur()">link1</a>
<a href="link2.htm" onfocus="this.blur()">link2</a>
<a href="link3.htm" onfocus="this.blur()">link3</a> 

  粗看或許還體現不出采用expression的優勢,但如果你的頁面上有幾十甚至上百個鏈接,這時的你難道還會機械式地Ctrl+C,Ctrl+V么,何況兩者一比較,哪個產生的冗余代碼更多呢?

  采用expression的做法如下: 

<style type="text/css">
a {star : expression(onfocus=this.blur)}
</style>
<a href="link1.htm">link1</a>
<a href="link2.htm">link2</a>
<a href="link3.htm">link3</a> 

  說明:里面的star就是自己任意定義的屬性,你可以隨自己喜好另外定義,接著包含在expression()里的語句就是JS腳本,在自定義屬性與expression之間可別忘了還有一個引號,因為實質還是CSS,所以放在style標簽內,而非s cript內。OK,這樣就很容易地用一句話實現了頁面中的鏈接虛線框的消除。不過你先別得意,如果觸發的特效是CSS的屬性變化,那么出來的結果會跟你的本意有差別。例如你想隨鼠標的移進移出而改變頁面中的文本框顏色更改,你可能想當然的會認為應該寫為 

<style type="text/css">
input 
{star : expression(onmouseover=this.style.backgroundColor="#FF0000";
onmouseout=this.style.backgroundColor="#FFFFFF")}
</style>
<style type="text/css">
input {star : expression(onmouseover=this.style.backgroundColor="#FF0000";
onmouseout=this.style.backgroundColor="#FFFFFF")}
</style>
<input type="text">
<input type="text">
<input type="text">

  可結果卻是出現腳本出錯,正確的寫法應該把CSS樣式的定義寫進函數內,如下所示:

<style type="text/css">
input {star : expression(onmouseover=function()
{this.style.backgroundColor="#FF0000"},
onmouseout=function(){this.style.backgroundColor="#FFFFFF"}) }
</style>
<input type="text">
<input type="text">
<input type="text"> 

  注意

  不是非常需要,一般不建議使用expression,因為expression對瀏覽器資源要求比較高。

實例:利用css里expression來實現界面對象的批量控制 

    問題說明: 用過CSS樣式我們就知道, 可以定義一批對象的class屬性來指定同一個樣式來統一界面. 但如何統一同類型的對象的事件? 比如:界面有無數個 <img src="**.jpg"> 如何實現鼠標經過此圖片, 圖片的src變成是**_over.jpg?

 
解決方法: 使用css的expression方法,
具體實現要看看.css的寫法:

/*替換圖片CSS*/
#imgScript {   /*這里使用對象ID來通配樣式, 也可以定義一個css函數*/
 star:expression(
    onmouseover = function()
    { 
        /*替換圖片*/
        if(this.hover != null){
          this.name = this.src;
          this.src = this.src.replace('.jpg', '_over.jpg'); 
          this.HasChg = 1;
       }
   },
   onmouseout = function()
  {  
       /*還原本來的圖片*/
     if(this.HasChg != null){
        this.src = this.name;
        this.HasChg = null;
    }
 }


}/*end imgScript*/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洱源县| 临泽县| 莒南县| 黑山县| 东莞市| 清远市| 南陵县| 榆社县| 阿克苏市| 绍兴县| 绥阳县| 黄骅市| 新干县| 那曲县| 洛川县| 庆阳市| 名山县| 甘洛县| 宁远县| 乳山市| 泰和县| 丹巴县| 石柱| 吴川市| 开鲁县| 瑞金市| 凉城县| 金坛市| 舞钢市| 察隅县| 涿州市| 涞源县| 牟定县| 宁陕县| 崇州市| 沁阳市| 康乐县| 秀山| 高平市| 湖北省| 贵港市|