圖1 圖書列表
?? ????????圖2 class和style的綜合應用 代碼可以截圖或者復制黏貼放置在“調試過程及實驗結果”中
圖3 輸出古詩 |
3. 補全代碼,頁面效果如下圖3所示 |
3. 補全代碼,頁面效果如下圖3所示 |
8.1 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>為指定書名添加顏色</title> <style> .item{ width:350px; height:100px; line-height:100px; border-bottom:1px solid #999999;} .item img{ width:100px; float:left} .active{ height:100px; line-height:100px; color:#FF0000 } </style> <script src="vue.js"></script> </head> <body> <div id="example"> ? ? <div> ? ? ? ? ? <div class="item" v-for="book in books"> ? ? ? ? ? ? ? <img v-bind:src="book.image"> ? ? ? ? ? ? ? <span v-bind:class="{active : book.active}">{{book.bookname}}</span> ? ? ? ? ? </div> ? ? </div> </div> <script type="text/javascript"> var vm = new Vue({ ? ? el:'#example', ? ? data:{ ? ? ? ? books : [{//定義圖書信息數組 ? ? ? ? ? ? bookname : '零基礎學JavaScript', ? ? ? ? ? ? image : 'images/javascript.png', ? ? ? ? ? ? active : true ? ? ? ? },{ ? ? ? ? ? ? bookname : '零基礎學HTML5+CSS3', ? ? ? ? ? ? image : 'images/htmlcss.png', ? ? ? ? ? ? active : false ? ? ? ? },{ ? ? ? ? ? ? bookname : 'JavaScript精彩編程200例', ? ? ? ? ? ? image : 'images/javascript200.png', ? ? ? ? ? ? active : false ? ? ? ? },{ ? ? ? ? ? ? bookname : 'HTML5+CSS3精彩編程200例', ? ? ? ? ? ? image : 'images/htmlcss200.png', ? ? ? ? ? ? active : true ? ? ? ? }] ? ? } }) </script> </body> </html>
8.2 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>模擬古詩垂直顯示文本</title> <script src="vue.js"></script> </head> <body> <div id="example"> ? ? <!-- ? ? ? ? 1、baseStyle設置背景色為lightblue, ? ? ? ? 2、fontStyle設置字體為36px,顏色為green,文本水平居中,字體類型為華文楷體 ? ? ? ? 3、styleRadius設置邊框樣式1px solid #CCCCCC,文本垂直排列,設置邊框陰影 ? ? --> ? ? <div v-bind:style="[baseStyle, fontStyle, styleRadius]"> ? ? ? ? <h4>楓橋夜泊</h4> ? ? ? ? <p> ? ? ? ? ? ? 月落烏啼霜滿天,<br>江楓漁火對愁眠。<br>姑蘇城外寒山寺,<br>夜半鐘聲到客船。 ? ? ? ? </p> ? ? </div> </div> <script type="text/javascript"> var vm = new Vue({ ? ? el:'#example', ? ? data:{ ? ? ? ? bgcolor : 'lightblue', ? ? ? ? family : "華文楷體", ? ? ? ? fontSize : 36, ? ? ? ? color : 'green', ? ? ? ? align : 'center', ? ? ? ? border : '1px solid #CCCCCC', ? ? ? ? mode : 'vertical-rl',//垂直方向自右而左的書寫方式 ? ? ? ? ? ? }, ? ? computed: { ? ? ? ? baseStyle: function () {//基本樣式 ? ? ? ? ? ? return{ ? ? ? ? ? ? ? ? 'background-color':this.bgcolor, ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? fontStyle: function(){//字體樣式 ? ? ? ? ? ? return{ ? ? ? ? ? ? ? ? fontSize:this.fontSize+'px', ? ? ? ? ? ? ? ? color:this.color, ? ? ? ? ? ? ? ? 'text-align':this.align, ? ? ? ? ? ? ? ? 'font-family':this.family, ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? styleRadius: function () { ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? border:this.border, ? ? ? ? ? ? ? ? 'writing-mode':this.mode, ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? } ? ? } }) </script> </body> </html>
8.3 <!-- vue-3-6.html --> <!DOCTYPE html> <html> ? ? <head> ? ? ? ? <meta charset="utf-8"> ? ? ? ? <title>class與style綁定綜合應用實戰</title> ? ? ? ? <script src="vue.js"></script> ? ? ? ? <style type="text/css"> ? ? ? ? ? ? .redP {color: red;font-size: 28px;font-weight: bold;} ? ? ? ? ? ? .class-a {color: green;font-size: 36px;font-weight: bolder;} ? ? ? ? ? ? .class-b {border: 1px dashed #0033CC;} ? ? ? ? ? ? .active {color: blue;text-decoration: underline;} ? ? ? ? </style> ? ? </head> ? ? <body> ? ? ? ? <div id="vue36"> ? ? ? ? ? ? <p v-bind:class="myClass">1.1 普通變量:Vue應用前景寬廣!</p> ? ? ? ? ? ? ? <!-- 1.2 使用非內聯的形式:classObject為一個對象,使用.class-a"和.class-b樣式 --> ? ? ? ? ? ? <p :class="classObject">1.2 對象-非內聯:Vue應用前景寬廣!</p> ? ? ? ? ? ? ?<!-- 1.3 使用內聯的形式:渲染.active樣式 --> ? ? ? ? ? ? <div v-bind:class="{active: isActive }">1.3 對象-內聯:Vue應用前景寬廣!</div> ? ? ? ? ? ? <!-- 1.4 使用計算屬性,渲染.class-a"和.class-b樣式 --> ? ? ? ? ? ? <div v-bind:class="showClass">1.4 對象-計算屬性:Vue應用前景寬廣!</div> ? ? ? ? ? ? <div v-bind:class="[classA, classB]">1.5 數組:Vue非常好學!</div> ? ? ? ? ? ? ? ? ? ? ? <div v-bind:style="{color:activeColor,fontSize:fontSize+'px'}">2.1 對象-內聯:綁定style</div> ? ? ? ? ? ? <!-- 2.2 使用對象-非內聯形式:通過styleObject對象實現字體大小為32px,邊框為2px藍色實線2px solid #0033CC --> ? ? ? ? ? ? <div :style="styleObject">2.2 對象-非內聯:style對象</div> ? ? ? ? ? ? <div v-bind:style="[styleObjectA, styleObjectB]">2.3 數組:style數組</div> ? ? ? ? ? ? <!-- 2.4 使用計算屬性,讓2.4文本的color為blue,fontSize為32px --> ? ? ? ? ? ? <div v-bind:style="showStyle">2.4 對象-計算屬性:Vue應用前景寬廣!</div> ? ? ? ? </div> ? ? ? ? <script type="text/javascript"> ? ? ? ? ? ? var myViewModel = new Vue({ ? ? ? ? ? ? ? ? el: '#vue36', ? ? ? ? ? ? ? ? data: { ? ? ? ? ? ? ? ? ? ? myClass: 'redP', ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? isActive:true, ? ? ? ? ? ? ? ? ? ? classA: 'class-a', ? ? ? ? ? ? ? ? ? ? classB: 'class-b', ? ? ? ? ? ? ? ? ? ? activeColor: '#99DD33', ? ? ? ? ? ? ? ? ? ? fontSize: 36, ? ? ? ? ? ? ? ? ? ? classObject:{ ? ? ? ? ? ? ? ? ? ? ? ? 'class-a':true, ? ? ? ? ? ? ? ? ? ? ? ? 'class-b':true, ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? styleObject: { ? ? ? ? ? ? ? ? ? ? ? ? border: '2px' + 'solid' + '#0033CC', ? ? ? ? ? ? ? ? ? ? ? ? fontSize: 32 + 'px', ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? styleObjectA: { ? ? ? ? ? ? ? ? ? ? ? ? color: 'blue', ? ? ? ? ? ? ? ? ? ? ? ? fontSize: 36 + 'px' ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? styleObjectB: { ? ? ? ? ? ? ? ? ? ? ? ? background: '#DFDFDF' ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? computed:{ ? ? ? ? ? ? ? ? ? ? showClass:function(){ ? ? ? ? ? ? ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'class-a':true, ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'class-b':true, ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? ? ? ? showStyle:function(){ ? ? ? ? ? ? ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? ? ? ? ? ? ? color:'blue', ? ? ? ? ? ? ? ? ? ? ? ? ? ? fontSize:'32px', ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? }) ? ? ? ? </script> ? ? </body> </html> |