2014年6月26日 星期四

舊版 IE 支援 HTML5、CSS3 的幾種方法

 

■ 透過 javascript 支援 HTML 5、CSS 3



使用 Remy 的 HTML5shiv -- htmlshiv.js

利用 JavaScript 在舊版 IE 建立 main、header、footer 等 HTML5 元素(elements)。

<!-​​-[if lt IE 9]>
<script src="dist/html5shiv.js"></script>
<![endif]-->


使用 selectivizr.js 可支援 CSS selectors、properties,例如 last-child

<!--[if lte IE 8]>
<script src="js/libs/selectivizr.js"></script>
<![endif]-->



■ 利用 IE 的條件式註解 <html> Conditional Comments



<!DOCTYPE html>
<!--[if lt IE 7 ]> <html class="ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8" lang="en"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9" lang="en"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->





參考
3 Solutions for Supporting Internet Explorer
http://davidwalsh.name/supporting-internet-explorer

2012年9月24日 星期一

Lava Lamp 模擬

Lava lamp 模擬


執行效果:

當滑鼠滑到表單時,下方有個指標會跟著移動


如果下方游標移動不正常,請再重新整理。因為上面案例並沒有把 jQuery 寫入 $(window).load(function() { }); 內,而是寫在 jQuery(document).ready(function(){ }); ,在某些瀏覽器下,例如 chrome,可能會在圖片載入完畢前就已經偵測圖片的錯誤位置。
實作上請寫入 $(window).load(function() { }); 內,或使用 deferred 判斷圖片載入是否完成。

程式重點:
1. 如何使用 jQuery 取得 menu 小圖示的相對位置
2. menu 小圖示的位置可紀錄在陣列、或 .data() 內、或物件內
    因位置不會變動,可紀錄在 .data() 內(避免全域名稱汙染--濫用或名稱衝突),此處是偷懶省步驟,所以紀錄在陣列。
    這步驟是設計關鍵絕竅,將 menu 位置或寬度紀錄下來,當滑鼠移入或移出某表單時,讀取紀錄值後移動位置。
3. 如何設計 menu 小圖示的 position



CSS
#wrapper {
        position: relative;
        float: right;      /* 靠右,隨便靠 */
        right: 10%;     /* 隨心設定 */
        width: 210px;   /* 所有MENU小圖示都可容納顯示即可 */
        height: 90px;    /* 若沒設定高度,滑鼠移過時 MENU 會變高,造成下方區塊內容往下移 */
        border: 1px solid black;  /* 可省略 */
}

#mainmenu img {
        position: relative;   /* 方便你取得各 image 相對位置用 */
        overflow: hidden;
}
#menuhover {
        position: relative;
        display: none;
}
HTML
<div id="wrapper">
        <div id="mainmenu">
                <img src="/photo/p01.png">
                <img src="/photo/p02.png">
                <img src="/photo/p03.png">
                <img src="/photo/p04.png">
        </div>
        <img id="menuhover" src="/photo/mousepointer.gif">
</div>
jQuery
var aryPosX = [];
$('#mainmenu img').each(function(){   
        aryPosX.push($(this).position().left);   
});   
$('#mainmenu img').mouseenter(function(){   
        var idx = $(this).index();    // 取得 img 在 #mainmenu 的位置順序,值為 0、1、2、3,配合從 aryPosX 陣列內取值   
        $('#menuhover')   
               .show()
               .stop(true, false)    // 注意這裡的 stop 設定   
               .animate({ 'left': aryPosX[idx] });   
});
通常實作上,會在圖片載入完畢後才去偵測圖片位置,你可以將上段程式碼寫在 $(window).load(function() { }); 內。 或使用 jQuery 的 deferred 判斷圖片是否載入完畢。 執行結果:
jsfiddle 看效果
jsfiddle 看效果
你可以自己修改程式碼後直接按 RUN 看看能不能跑(不要 update)。


請注意,在某些瀏覽器下,例如 chrome,須將 jsfiddle.net 的左側的 onDomReady 改為 onLoad,才能正常顯示。



國外範例(下方長條圖或背景方框變動也是類似道理。)
LavaLamp for jQuery lovers
CSS+Javascript power. Fancy menu
Simple Lava Lamp Menu Tutorial with jQuery


2012年9月20日 星期四

jQuery 圖片或文字依序顯示



jQuery 圖片或文字依序顯示程式完成結果預覽

如果上方iframe有錯誤,請直接點擊:點擊預覽
---------------------------------------------------------
[特效] 圖片依序顯示。
[重點] 使用 arguments.callee 呼叫匿名函式。




步驟解說
[步驟一] 鍵入以下程式
CSS
/* 設定圖片隱藏 */
.productdemo img {
        display:none;
}
HTML

<div class="productdemo">
        <img src="/photo/slider01.png">
        <img src="/photo/slider02.png">
        <img src="/photo/slider03.png">
        <img src="/photo/slider04.png">
        <img src="/photo/slider05.png">

</div>

<button id="play" type="button">play</button>
javascript - jQuery
/* 圖片依序顯示函數 */
function prodFadeIn() {
    $('.productdemo img').first().fadeIn(1000, function() {
        $(this).next().fadeIn(500, arguments.callee);
    });
}

$(window).load(function() {
    // 設定按鈕的 click 事件監聽
    $('#play').click(function() {
        $('.productdemo img').hide();
        prodFadeIn();
    });
});



結果: 前往 jsfiddle.net 見效果
觀看效果請點『 play按鈕』



---------------------------------------------------------------------
[步驟二] 但是當你一直點 『play按鈕』,你會發現圖片沒有依序顯示,而是前後亂跳,這時主要有 2 個解決方法

解決方法
[解1]. 在 fadeOut 前加 stop(),但參數的 clear queues、jump to end 必須為 true,也就是 stop(true, true),否則當一直點選 button 時,在快速切換下前幾張圖容易出現淡化或隱藏情況。
javascript - jQuery
function prodFadeIn() {
    $('.productdemo img').first().fadeIn(1000, function() {
        $(this).next().fadeIn(500, arguments.callee);
    });
}

$(window).load(function() {
    $('#replay').click(function() {
        $('.productdemo img').stop(true, true).hide();     // animate 特效前先呼叫 stop(clear-queues, jump-to-end)
        prodFadeIn();
    });
});


前往 jsfiddle.net 見效果



[解2]. 或是圖片未顯示完畢前先disable button,避免使用端一直按按鈕。


[最終解].解決方案 1、2 一起使用
javascript - jQuery
function prodFadeIn(){
    // loopCount: 第幾張圖片開始顯示, loopEnd: 圖片全部數目
    var loopCount = 1, loopEnd = $('.productdemo img').length;

    $('.productdemo img').first()
        .fadeIn(1000, function(){

            // 圖片全部顯示完後,play 按鈕才取消 disable 屬性
            if (loopCount >= loopEnd){
                $('#replay').removeAttr('disabled');
            }
            $(this).next()
                .fadeIn(1000, arguments.callee);
            ++loopCount;    // 計算顯示到第幾張圖片
        });
}

$(window).load(function(){
    $('#replay').click(function(){
            $('.productdemo img').stop(true, true).hide();
            $('#replay').attr('disabled', 'true');
            prodFadeIn();
    });
});


範例 前往 jsfiddle.net 見效果




-----------------------------------------------------------------------
簡單應用 1

圖片由右至左移動

[解說]
[步驟1]. 假設有 4 張圖片,CSS & HTML 設定如下:
CSS
.productanimate { 
     position: relative;   /* 非必要 */
     width : 150px; 
     height: 60px; 
     border:1px solid black;  /* 非必要 */
     overflow: hidden; 
} 
.productanimate img { 
     display : none;
     position: absolute; 
}


HTML
<div class=productanimate>
    <img id=ma1 src="/photo/a01.png">
    <img id=ma2 src="/photo/a02.png">
    <img id=ma3 src="/photo/a03.png">
    <img id=ma4 src="/photo/a04.png">
</div>


[步驟2]. 用 jQuery 針對每張圖片設定它的 x 軸位置
$('.productanimate img').show().each(function(i){
      $(this).css({ 
             // 設定每張圖片 X 軸起始位置 ---自行調整,例如間隔 20px 
             left: i* 20 , 
             opacity : 0.2 
     });
}); 

[步驟3]. 移動第一張圖片
$('.productanimate img').first() 
      .animate({opacity : 1}, 500) 
      .delay(1000) 
      .animate( { left: 移出距離 }, 1200 , function(){ 
          code here.......... // 見步驟 4
     }); 



[步驟4]. 移動第一張圖片後要接著做的動作: 依序移出下一張圖片
$(this).next() 
     .animate({opacity : 1}, 500) 
     .animate({ left: 每張圖片移動距離 }, 移出所須時間, 動作完畢所要執行的函式);




完整程式碼
CSS
.productanimate {
    position: relative; 
    width : 150px; 
    height: 60px; 
    border:1px solid black; 
    overflow: hidden; 
} 
.productanimate img { 
    display : none; 
    position: absolute; 
}
HTML
<div class="productanimate">
    <img src="http://placehold.it/30x30">
    <img src="http://placehold.it/30x30">
    <img src="http://placehold.it/30x30"> 
    <img src="http://placehold.it/30x30"> 
</div>
<button id="play" type="button">play</button>
<div id="info"></div>
javascript
function func() {
 
 // Step 1: 宣告變數 
 // x: 每張圖片移出距離 
 // duration: 每張圖片移出要耗費的秒數,愈後面的圖片因為移動距離比較大,要指定比較長的移出時間。視情況須自行調整。 
 // loopcount: 判斷所有圖片是否已經全部移出 
 // distance: 設定圖片定位點用。視情況自行調整。 
 var x = $('.productanimate img').first().outerWidth(true), 
  duration, 
  loopcount = 0, 
  loopMax = $('.productanimate img').length - 1, 
  distance = x / 2, 
  debuginfo = ''
  ;

 // Step 2: 設定圖片初始值:位置與透明度等 
 $('.productanimate img').show().each(function(i) {
  $(this).css({
   // 每張圖片間格為圖片寬度的一半  
   left: i * distance,
   opacity: 0.5
  });
 });

 // Step 3: 開始圖片移動 
 $('.productanimate img')
  .first() // 先移動第一張圖片 
  .animate({ opacity: 1 }, 500).delay(1000).animate({ left: '-' + x + 'px' }, 200, function() {

   // 匿名函式 --//START\\--------------------------------------- 
   // 移動第一張圖片後要接著做的動作  
   x += distance;
   loopcount = (loopcount > loopMax) ? 0 : loopcount; // 如果全部圖片都移出,則重新計數 
   duration = 500 + loopcount * 200; // 每張圖片移出時間,若要看起來等速移出,愈後面的圖片花的時間會比較長 


   if (loopcount >= loopMax){ // 檢查圖片是否全部移出,判斷是否停止圖片依序移出效果,並恢復按鈕功能
    $('#play').attr('disabled', false);
   }
   ++loopcount;


   // debug --//START\\--------------------- 
   debuginfo += '圖片: ' + loopcount + ', 移動距離: ' + x + ', 移動時間: ' + duration + '\n';    //「\n」自行替換為 HTML 的  br 標籤(因 br 標籤在 syntaxhighlight 內無法正常顯示,故此處以 \n 代替
   $('#info').html(debuginfo);
   // debug --\\END//---------------------

  
   $(this).next()
    .animate({ opacity: 1 }, 500)
    .animate({ left: '-' + x + 'px' }, duration, arguments.callee);
   // 匿名函式 --\\END//--------------------------------------

 });
};


$(window).load(function() {
 $('#play').click(function(){
  $(this).attr('disabled', true); // 按鈕 disable
  func();
 });
});



結果: 前往 jsfiddle.net 見效果 執行請點 【Run】


---------------------------------------
簡單應用 2

文章段落依序顯示


CSS
HTML
<div id="paracht">
     <p>文章段落一</p> 
     <p>文章段落二</p> 
     <p>文章段落三</p> 
     <p>文章段落四</p> 
     <p>文章段落五</p> 
     <p>文章段落六</p> 
     <p>文章段落七</p> 
     <p>文章段落八</p> 
</div>
<button id="parachtshow" type="button">show paragraphs in sequence...</button>


jQuery
var fadeinTime = 300;
$('#paracht p').hide();

$('#parachtshow').click(function(){
       $('#paracht p').first().stop(true, true).fadeToggle(fadeinTime, function() {
             $(this).next().stop(true, true).fadeToggle(fadeinTime, arguments.callee);
       });
 });


結果:
前往 jsfiddle.net 見效果
執行請點 【Run】

-----------------------------------------------------------------------
檔案下載
下載(單一HTML 檔案)

更新版本(程式碼更新,圖片檔+HTML檔案)