:: Kunagorn Sirikup :: C# Developer

แสดงบทความที่มีป้ายกำกับ CSS แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ CSS แสดงบทความทั้งหมด

6/1/57

การแสดงผล CSS3 บน INTERNET EXPLORER ด้วย PIE


ในปัจจุบันมีเทคโนโลยีมากมายที่ช่วยให้เราสามารถออกแบบเวบไซต์ได้สะดวกและรวดเร็วมากขึ้น CSS (cascade style sheet) ก็เป็นอีกทางเลือกหนึ่งที่ช่วยเพิ่มความสวยงามให้กับเวบไซต์ รวมถึงสามารถแก้ไขหรือปรับปรุงหน้าเพจได้ง่าย เพราะรูปแบบการแสดงผลมาจากคำสั่ง Style Sheet ชุดเดียวกัน
ปัจจุบันได้มีการพัฒนา CSS3 เพื่อให้มีความสามารถเพิ่มขึ้นจาก CSS2 โดยเพิ่มฟีเจอร์ต่างๆขึ้น เช่น Border-Radius ,  Multiple Backgrounds เป็นต้น (สามารถดูเพิ่มเติมได้ที่ http://www.css3.info/preview ) อย่างไรก็ดี CSS3  ก็มีข้อเสียตรงที่ ไม่สามารถรองรับได้ทุกเว็บบราวเซอร์ โดยเฉพาะ Internet Explorer ดังนั้น  จึงได้มีการพัฒนา PIE (Progressive Internet Explorer) เพื่อเป็น Extensions ที่ช่วยให้ Internet Explorer ตั้งแต่รุ่น 6.x - 8.x สามารถแสดงผลหน้าเว็บที่ใช้ CSS3 ได้
ขั้นตอนการใช้PIE
1. ดาวน์โหลดไฟล์ PIE.htc (สามารถดาวน์โหลดได้จาก http://css3pie.com )
2. นำไฟล์ PIE.htc มาใส่ไว้ใน Solution ของเว็บไซต์ โดยเก็บไว้ใน Directory เดียวกันกับ StyleSheet ดังนี้
3. ใน StyleSheet ที่มีการใช้ Properties ของ CSS3 ให้เขียน Properties behavior ลงไป ตัวอย่าง  behavior: url(PIE.htc); โดยระบุ Path ที่เข้าถึง PIE.htc ให้ถูกต้อง ดังนี้
4. ในตัวอย่างนี้จะเกิดช่องสี่เหลี่ยมที่มีขอบโค้งมลและมีเงา ซึ่งเป็นคุณสมบัติของ CSS3 ซึ่งไม่สามารถแสดงบน IE8 ได้  แต่เมื่อเรากำหนด Properties behavior ของ PIE ลงไปก็สามารถแสดงผลได้ปกติ โดยไม่ต้องแก้ไข CSS

21/11/56

FREEZE HEADER COLUMN เลื่อนตามหน้าจอ

วันนี้มาเขียนวิธีการสร้าง Table สำหรับแสดงข้อมูลบนหน้าเว็บ แล้วกำหนดให้ Table แสดง Header Column เลื่อนตามลงเมื่อเรา Scroll หน้าจอลงมา ซึ่งจะได้หน้าตาออกมา ดังรูป


















วิธีการสร้างไม่ยากเลยครับมาลองทำกันทีละขั้นตอนนะครับ
เริ่มแรกผมจะสร้าง CSS ขึ้นมา ดังนี้นะครับ
?
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
43
44
45
46
47
48
49
50
51
<style>
    * {
        margin: 0;
        padding: 0;
    }
    body {
        font: 14px Georgia, serif;
    }
    h1 {
        font: 32px Georgia, Serif;
        margin: 0 0 20px 0;
    }
    p {
        margin: 0 0 20px 0;
    }
    #page-wrap {
        width: 500px;
        margin: 10px auto;
        padding:10px;
    }
    table {
        border-collapse: collapse;
    }
    th {
        background-color: lightgrey;
        padding: 10px;
        width: 200px;
        text-align: left;
    }
    tr:nth-child(odd) {
        background: #eee;
    }
    td {
        padding: 10px;
        width: 200px;
    }
    .floatingHeader {
        position: fixed;
        top: 0;
        visibility: hidden;
    }
</style>

ในตัวอย่างข้างบนเป็น CSS มี class ชื่อ “floatingHeader” ที่ใช้ในการสร้าง “Freeze Header Column” นอกนั้นผมสร้างขึ้นมาเพื่อใช้ในตัวอย่างนี้เท่านั้น
ต่อมาก็มาเขียน Javascript ดังนี้
?
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
<script>
    function UpdateTableHeaders() {
        $(".persist-area").each(function () {
            var el = $(this),
                offset = el.offset(),
                scrollTop = $(window).scrollTop(),
                floatingHeader = $(".floatingHeader", this)
            if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) {
                floatingHeader.css({
                    "visibility": "visible"
                });
            } else {
                floatingHeader.css({
                    "visibility": "hidden"
                });
            };
        });
    }
    // DOM Ready     
    $(function () {
        var clonedHeaderRow;
        $(".persist-area").each(function () {
            clonedHeaderRow = $(".persist-header", this);
            clonedHeaderRow
                .before(clonedHeaderRow.clone())
                .css("width", clonedHeaderRow.width())
                .addClass("floatingHeader");
        });
        $(window)
            .scroll(UpdateTableHeaders)
            .trigger("scroll");
    });
</script>

จาก Code ข้างบนจะเห็นว่า Javascript จะทำงานเมื่อมีการ Scroll หน้าจอลงมา แล้วไปเรียกฟังก์ชั่น UpdateTableHeaders เพื่อทำการสร้าง Header Column ที่จะวิ่งตามการเลื่อนของ Scrollbar
จากนั้นก็สร้าง HTML ดังนี้
?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<div id="page-wrap">
    <table class="persist-area">
        <thead>
            <tr class="persist-header">
                <th>ID</th>
                <th>Name</th>
                <th>Position</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>EMP0001</td>
                <td>Mr.Kunagorn Sirikupt</td>
                <td>Developer</td>
            </tr>
            <tr>
                <td>EMP0002</td>
                <td>Mr.AAAAAAAAAAAAA</td>
                <td>System Analysis</td>
            </tr>
            <tr>
                <td>EMP0003</td>
                <td>Mr.BBBBBBBBBBBBBBBB</td>
                <td>Developer</td>
            </tr>
            <tr>
                <td>EMP0004</td>
                <td>Mr.CCCCCCCCCCCCCCCC</td>
                <td>System Analysis</td>
            </tr>
            <tr>
                <td>EMP0005</td>
                <td>Mr.DDDDDDDDDDDDDDDDDD</td>
                <td>Developer</td>
            </tr>
            <tr>
                <td>EMP0006</td>
                <td>Mr.EEEEEEEEEEEEEEEEEE</td>
                <td>System Analysis</td>
            </tr>
            <tr>
                <td>EMP0007</td>
                <td>FFFFFFFFFFFFFFFFFFF</td>
                <td>Developer</td>
            </tr>
            <tr>
                <td>EMP0008</td>
                <td>GGGGGGGGGGGGGGGGGG</td>
                <td>System Analysis</td>
            </tr>
            <tr>
                <td>EMP0009</td>
                <td>HHHHHHHHHHHHHHHHH</td>
                <td>System Analysis</td>
            </tr>
            <tr>
                <td>EMP0010</td>
                <td>KKKKKKKKKKKKKKK</td>
                <td>Developer</td>
            </tr>
            <tr>
                <td>EMP0001</td>
                <td>LLLLLLLLLLLLLLLLL</td>
                <td>Developer</td>
            </tr>
        </tbody>
    </table>
    <div style=" margin-top:20px;">
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
            Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero
            sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
            Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed,
            commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum,
            eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar
            facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue,
            eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis,
            accumsan porttitor, facilisis luctus, metus</p>
    </div>
    <div>
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
            Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero
            sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
            Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed,
            commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum,
            eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar
            facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue,
            eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis,
            accumsan porttitor, facilisis luctus, metus</p>
    </div>
</div>

จาก Code ข้างบนมีข้อสังเกต ดังนี้
- Table ที่สร้างต้องมีการประกาศ class ชื่อ “persist-area”
- ในส่วนของ Header Column ต้องใช้ tag <th></th>
?
1
2
3
4
5
6
7
<thead>
    <tr class="persist-header">
        <th>ID</th>
        <th>Name</th>
        <th>Position</th>
    </tr>
</thead>