Skip to content

有害部落格同好會

2005/11/29 / JavaScript, VBScript

製作載入圖片的效果

不使用 Flash ,製作載入圖片的效果

資料來源:Onload image fades without Flash

The code :


The photo is a straightforward image in a div.
Each has an id:

'Photo'

The ‘loading…’ image is set as the background to the photoholder div, and the photoholder is sized to match the photo.

#photoholder {
  width:450px;
  height:338px;
  background:#fff url('/images/loading.gif') 50% 50% no-repeat;
}

#thephoto {
  width:450px;
  height:338px;
}

To prevent a cached photo from displaying before it has been faded in, when need to make the photo hidden. JavaScript is used to write a style rule so that users without JavaScript enabled will not have the photo permanently hidden:

document.write("");

Note that document.write does not work when XHTML is properly sent as application/xhtml+xml. You will either have to use HTML or serve your document as text/html.

Once everything on the page has loaded (crucially this includes images), an onload event is triggered, calling our initImage function:

window.onload = function() {initImage()}

The initImage function makes the photo completely tranpsarent by using the setOpacity function to set its opacity to zero. The photo can then be made visible and faded in using the fadeIn function:

function initImage() {
  imageId = 'thephoto';
  image = document.getElementById(imageId);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  fadeIn(imageId,0);
}

The setOpacity function is passed an object and an opacity value. It then sets the opacity of the supplied object using four proprietary ways. It also prevents a flicker in Firefox caused when opacity is set to 100%, by setting the value to 99.999% instead.

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
// IE/Win
obj.style.filter = "alpha(opacity:"+opacity+")";

// Safari<1.2, Konqueror
obj.style.KHTMLOpacity = opacity/100;

// Older Mozilla and Firefox
obj.style.MozOpacity = opacity/100;

// Safari 1.2, newer Firefox and Mozilla, CSS3
obj.style.opacity = opacity/100;
}

The fadeIn function uses a Timeout to call itself every 100ms with an object Id and an opacity. The opacity is specified as a percentage and increased 10% at a time. The loop stops once the opacity reaches 100%:

function fadeIn(objId,opacity) {
if (document.getElementById) {
  obj = document.getElementById(objId);
  if (opacity < = 100) {
    setOpacity(obj, opacity);
    opacity += 10;
    window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
  }
}
}

Accessibility

This implementation caters for all combinations of JS and CSS; that is to say the photo can always be seen once it has loaded.

Post navigation

Next Post:

清除多餘html代碼

分類

  • 新奇有趣の搶先報導
    • Raspberry Pi
    • 手機相關
    • 推薦軟體
    • 新鮮有趣
    • 生活資訊
  • 想破腦袋の程式技巧
    • Oracle EBS
    • Database
    • Excel, VBA
    • php
    • JavaScript, VBScript
    • VS.NET
    • Others
    • Windows
    • SAP
  • 撩動心弦の短文小品
  • 聚沙成塔の理財守則
  • 不可不知の職場實錄
  • 剎那永恆の生活翦影

近期文章

  • 受保護的內容: 如何透過Personalize功能呼叫另一form來回傳值
  • Win10 / 8 / 7 無法安裝 SSD
  • 受保護的內容: 樹梅派+遠端連線
  • EBS R12 取得客戶的phone, email, URL資料
  • 受保護的內容: 管控Workflow Administrator Role

友站

  • Masaya396's 協奏曲
  • 老塗的咁仔店

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

Tag Cloud

你目前使用的瀏覽器不支援 HTML5 的 CANVAS 標籤。

  • WinXP
  • 管理
  • EBS 12.1.3
  • SQL
  • javascript
  • SAP
  • php
  • VBA
  • 感情
  • MySql
  • Win7
  • VB6
  • HTC
  • Oracle DB
  • EBS 11.5.10
  • LDAP
  • Oracle EBS
  • excel
  • VB.Net
  • CSS
© 2025 有害部落格同好會 - Powered by SimplyNews