js去除瀏覽器預設底圖的方法

來源:互聯網
上載者:User

js去除瀏覽器預設底圖的方法

   本文執行個體講述了js去除瀏覽器預設底圖的方法。分享給大家供大家參考。具體分析如下:

  我們在設計一些圖片比較多的網頁時,為了增強使用者體驗,希望圖片載入的時候有個loading動畫效果,而不是由空白到一下子出來。

  在zen cart的二次開發過程中同樣也遇到了這個問題,下面是我的解決方案。

  首頁給圖片設定一個預設的loading動畫,再分配一個id,

  如實際上載入過程通過一個函數來完成

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

function addListener(element, type, expression, bubbling) {

bubbling = bubbling || false;

if(window.addEventListener) { // Standard

element.addEventListener(type, expression, bubbling);

return true;

}

else if(window.attachEvent) { // IE

element.attachEvent('on' + type, expression);

return true;

}

else return false;

}

var ImageLoader = function(url){

this.url = url;

this.image = null;

this.loadEvent = null;

};

ImageLoader.prototype = {

load:function(){

this.image = document.createElement_x('img');

var url = this.url;

var image = this.image;

var loadEvent = this.loadEvent;

addListener(this.image, 'load', function(e) {

if(loadEvent != null){

loadEvent(url, image);

}

}, false);

this.image.src = this.url;

},

getImage:function(){

return this.image;

}

};

function loadImage(objId,urls) {

var loader = new ImageLoader(urls);

loader.loadEvent = function(url){

obj = document.getElementByIdx_x(objId);

obj.src = url;

}

loader.load();

}

  通過loadImage函數就可以為指定的圖片添加載入過程,其中通過addListener 函數註冊事件,

  使得在圖片載入完成的時候能夠自動替換掉loading.gif這個動畫過渡圖片。使用代碼很簡單

  ?

1

2

3

4

<img id="loading1″ src="loading.gif" />

<script language="javascript">

loadImage("loading1″,"http://www.baidu.com/img/baidu_logo.gif");

</script>

  希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.