顯示具有 JQuery 標籤的文章。 顯示所有文章
顯示具有 JQuery 標籤的文章。 顯示所有文章

2016年1月29日 星期五

[CSS]利用CSS3與JQuery達成開合面板特效

測試的chrome版本: 48.0.2564.97
其他版本或瀏覽器, 可能會有呈現上的差異, 請適當的修改各處的CSS

目的:用一張圖靜態的圖搞定特效

1.滑鼠移入時三角形變亮 
2.點擊時, 旋轉三角形與展開區域









下面是原料的原始碼

●CSS
    <style type="text/css">
        /*補間動畫*/
        @keyframes arrow {
        0%   { transform:rotate(0deg);}   /*初始的呈現*/
        100% { transform:rotate(90deg); -webkit-filter: brightness(200%);} /*最終的呈現*/
        }
        @keyframes arrow_close {
        0%   { transform:rotate(90deg);}
        100% { transform: rotate(0deg); -webkit-filter: brightness(100%);}
        }

        /*圖片套用的樣式*/
        .test-arrow {
            animation-name: arrow; /*使用的補間動畫*/
            animation-duration: 0.5s; /*動畫時間長度*/
            transform:rotate(90deg); /*相對於原圖的旋轉角度*/
            -webkit-filter: brightness(200%); /*相對於原圖的亮度*/
        }

        .test-arrow-close {
            animation-name: arrow_close;
            animation-duration: 0.5s;
            transform:rotate(0deg);
            -webkit-filter: brightness(100%); /**/
        }
    </style>

●混雜著asp.net的html(編譯到網頁時, 還是會回到html)
         <table class="fastFunc" style="width: 90%;">
           <tr style="background-color: #7292CC; color: #FFFFFF;">
                <td>
                    <img id="i_arrow" class="i_toggle" src="../BlueTriangle_glow.png" />快速停用
                </td>
           </tr>
           <tr>
<td>
               <asp:Panel id="arrow" CssClass="p_toggle" runat="server" BackColor="#C6DAF8">
                  //(自由發揮)
</asp:Panel>
               </td>
</tr>
        </table>


 重這邊開始有區別, 這裡是jQuery與js主要運作的地方

第一種, 較直觀的方法(直接指定ID)

優:簡單不容易指錯, 在測試和規劃初期可以減少需排除的錯誤, 相當適合只用一次的功能
缺:想重複使用功能時, 會需要寫進所需的code, 讓檔案大幅成長

    <script type="text/javascript">
        $(document).ready(function () {
            $("#arrow").hide();
            $("#i_arrow").mouseover(function () { $(this).css("-webkit-filter", "brightness(200%)") })
            $("#i_arrow").mouseout(function () { $(this).css("-webkit-filter", "brightness(100%)") })

            $("#i_arrow").click(function () {
                $(".fastFunc").slideToggle(300);
                if (this.className == "test-arrow") {
                    this.className = "test-arrow-close";
                }
                else
                    this.className = "test-arrow";
            });
        });
    </script>


第二種, 較抽象的方阿 (利用this與遍歷)

優:只需寫一次, 並透過適當的條件選擇, 就能套用在不同區塊
缺:不熟悉selector或架構尚未定型的開發中頁面, 使用此方法反而會增加困擾

    <script type="text/javascript">
        $(document).ready(function () {
            $(".p_toggle").hide();
            $(".fastFunc .i_toggle").mouseover(function () { $(this).css("-webkit-filter", "brightness(150%)") })
            $(".fastFunc .i_toggle ").mouseout(function () { $(this).css("-webkit-filter", "brightness(100%)") })

            $(".fastFunc .i_toggle").click(function () {
                $(this).parents("tr").next().find(".p_toggle").slideToggle(300);
                if (this.className == "test-arrow")
                    this.className = "test-arrow-close";
                else
                    this.className = "test-arrow";
            })
        });
    </script>

可以注意看, 其實程式碼差不多, 而是差別在selector的寫法

2015年11月24日 星期二

[JQuery]視條件隱藏GridView中同列首欄的內容


功能的訴求,是送出後仍能檢視,但不能再做修改(除非被更高權限的退回)
(大致上如下圖, 點下「to QC」後,會變成「Keyin OK!」,且轉為唯讀模式)

$(".AR_OK").css("color",
  function () {
        if ($(this).text() > 0) {
            $(this).prev().hide();
            $(this).text("Keyin OK!");

            $(this).parents("tr").first().find(":image").hide();
            return "green";
        }
        else {
            $(this).hide();
            return "black";
        }
    }
 )



這功能的實作,是利用class為「AR_OK」觸發
原本應該是「$(selector).css(attr, value)」,在給value值時,可以利用function(){}做變化
因為不是統一變化, 故有用到「$(this)」,對於選擇性變化的很好用
是沒用each()的另一種遍歷

【Step欄位】
先做if判斷, 是true則轉為唯讀並用綠色的「Keyin OK!」提示使用者, 並隱藏「to QC」的按鈕
如果判斷false, 則不顯示文字,留著「to QC」的按鈕

【第一欄】
四個選一個用吧, 越上面是越土法煉鋼
由於我要隱藏的剛好都是「input type=image」
Selector的部分可以用「:image」直接撈

$(this).parent().prev().prev().prev().prev().prev().prev().prev().children().hide();
$(this).parent().parent().first().children().children(":image").hide();
$(this).parents("tr").first().children().children(":image").hide();
$(this).parents("tr").first().find(":image").hide();

利用.parents("tr")一直退到<tr>
.first()表示要第一個
然後用find(":image")省略n次的children()直接到元素
至於.hide()就隨喜好了, 要改變style或繼續串燒都可以直接接

2015年10月13日 星期二

[JavaScript]可顯示英文大寫月份, 僅輸入時間可存完整datetime

在Actual Time僅輸入時間後, 能在DB中存入完整日期

DayDateNo.Actual TimeComments
1Dec/31/20151
2Jan/1/20162
5Jan/4/20163



【Read】(使用者要求要顯示MMM/dd/yyyy)
Actual Time:<asp:Label ID="D1Label" runat="server" Text='<%# Bind("D1","{0:HH:mm}") %>' />
Dosing Date:<asp:Label ID="CaptionD1" runat="server" Text="D1:"></asp:Label>
●後台
從後台撈資料出來後放入Json,再丟到前台進行格式化處理
由於涉及安全性,程式碼不完全公開
        DataTable dt = ds.Tables.Count > 0 ? ds.Tables[0] : new DataTable(); //將查詢結果放到DataTable
        string str_json = JsonConvert.SerializeObject(dt, Formatting.Indented); //將DataTable的內容轉換成JSON格式
        PdD.Text = str_json; //將JSON格式(文字)傳遞到前台

●前台
將抓到的月份轉換成使用者想看的
            calDate(); //呈現計算後的Dosing Date
         
            function calDate() { //日期計算
                var month = new Array(12);
                month[0] = "Jan";
                month[1] = "Feb";
                month[2] = "Mar";
                month[3] = "Apr";
                month[4] = "May";
                month[5] = "Jun";
                month[6] = "Jul";
                month[7] = "Aug";
                month[8] = "Sep";
                month[9] = "Oct";
                month[10] = "Nov";
                month[11] = "Dec";

                    var d = new Date(eval("jsonP[0].Period" + $("#lbPeriod").text() + "_dose"));
                    d.setDate(d.getDate() + Number($("#FormView1_CaptionD1).text()) - 1); //Dosing Date = Period + (Day-1)
                    var strD;
                    if (d.getFullYear() >= 2000)
                        strD = month[d.getMonth()] + "/" + +d.getDate() + "/" + d.getFullYear();
                    else
                        strD = "Undefined";
                    $("#FormView1_DateLabel").text(strD); //顯示使用者想看的格式
                }

藍字的:這時還是顯示數字,作用是依照Peroid與計算的天數差,顯示預計的日期
橘子的:將數字表示的日期,置換月份(js不自帶日期加減,需各自get再串接),>=2000的作用是避免使用者未設定日期(未設定日期的年份為19xx)

foreach的變化型就賣個關子啦


【Write】
1. 設定輸入欄位的屬性(多一個隱藏欄位)
Actual Time: <asp:TextBox class="HHmm" ID="D1TextBox" runat="server" Text='<%# Eval("D1","{0:HHmm}") %>' /></asp:TextBox>
Actual Time(隱藏的): <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("D1") %>'></asp:TextBox>

2. 隱藏欄位
將「 $(".HHmm").next().hide();」放在「  $(document).ready(function () { ... })」中

3. 利用「jquery.maskedinput.js」的功能來產生遮罩與限制輸入
            function setMasked() {
                $(".HHmm").mask("99:99", {
                    placeholder: "hh:mm", completed: function () {}
             });

4. 在「completed: function () { ... }」中放入輸入完成時要執行的動作,也就是當輸入完成時,將使用者輸入的稍加處理,放入實際上要回存DB的欄位
            $(this).next().val( //改變輸入框的值
              $(this).parent().prev().prev().children().text()  //抓取Dosing Date的日期
              + " " //在日期與時間之間加個空白
              + $(this).val() //取得使用者輸入的時間
            );

參考資料:JavaScript Date() 日期與時間

2015年9月15日 星期二

[JQuery]取得當下DropDownList選取的值(不需Postback)

$('#CountyDDL option:selected').val();
↑這方法想取得選取後的值, 必須要Postback
這不是我想要的

如果想隨時切換, 請愛用this (radio button也適用)
   <script type="text/javascript">
            $(document).ready(function () {
                var index = $("#FormView1_Label").text() - 1; //利用此變數, 控制顯示的時期
                if (index == -1) index = 0;
                    $("#FormView1_Caption").text(index);
                $("#FormView1_TextBox").change(function () {
                    index = $(this).val()-1; //此處使用「$(this).val()」是重點。「-1」單純是運算需要
                    $("#FormView1_Caption").text(index);
                });
            })

2015年6月13日 星期六

[JQuery]選擇同一列, 第n欄的第m個元素


DayNo.IntervalsTimeActualNote
-11-2507 : 0007:00
-12-24.507 : 3007:30


如果是Label就用.text();, 如果是TextBox就用val();
function(){}的外殼省略, 直接寫內容

1. 抓同欄的前一個
$(this).next().text();

2. 抓前一欄
$(this).parent().prev().children().text();
$(this).parent().prevAll(":last").children().text();
//二選一, 以此例而言, 結果相同
//而「parent().prev().children()」或「parent().prevAll().children()」,的作用, 是要先退到外層 移動, 再進到裡面抓東西

3. 抓第一欄
$(this).parent().prevAll(":last").children().text();
//別懷疑, 用prevAll()抓第一欄要用":last", 有點像感染擴散的方式

4. 抓第n欄, 第m個元素(例如Note為起點)
$(this).parent().prevAll(":eq(2)").children(":eq(0)").text();
//第一順位為0, Actual=prevAll("eq(0)"), Time=prevAll(":eq(1)")...

----
那不同列呢? 其實只要再往外退
用「.parent().parent()」或是「.parentsUntil("tr")」都可以 :D
鑽進去的方法也只是children()的延伸而已喔~

不過鑽的那層, 需要影響複數特定標籤的話, 可以用「>」 (ex:"tr>td")
這樣底下所有的<td>都會套用到喔