一甲易学 發表於 2019-7-19 11:18:00

Angular----样式

<p>本篇根据Angular官网提供的例子,对Angular涉及到的样式绑定进行说明。</p>
<p>一、提供的CSS样式</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(128, 0, 0, 1)">.red</span>{
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(255, 0, 0, 1)">color</span>:<span style="color: rgba(0, 0, 255, 1)">red</span>;
<span style="color: rgba(0, 128, 128, 1)"> 3</span> }
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(128, 0, 0, 1)">.green</span>{
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(255, 0, 0, 1)">color</span>:<span style="color: rgba(0, 0, 255, 1)"> green</span>;
<span style="color: rgba(0, 128, 128, 1)"> 6</span> }
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(128, 0, 0, 1)">.yellow</span>{
<span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(255, 0, 0, 1)">color</span>:<span style="color: rgba(0, 0, 255, 1)"> yellow</span>;
<span style="color: rgba(0, 128, 128, 1)"> 9</span> }
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(128, 0, 0, 1)">.size</span>{
<span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(255, 0, 0, 1)">font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 20px</span>;
<span style="color: rgba(0, 128, 128, 1)">12</span> }
<span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(128, 0, 0, 1)">.back</span>{
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(255, 0, 0, 1)">background-color</span>:<span style="color: rgba(0, 0, 255, 1)"> gray</span>;
<span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(255, 0, 0, 1)">opacity</span>:<span style="color: rgba(0, 0, 255, 1)"> 0.4</span>;
<span style="color: rgba(0, 128, 128, 1)">16</span> }</pre>
</div>
<p>二、绑定样式</p>
<p>(1)、传统的样式绑定-------绑定red&nbsp; size&nbsp; &nbsp;back</p>
<p>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)">class</span><span style="color: rgba(0, 0, 255, 1)">="red size back"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>传统的样式绑定<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></span>&nbsp;</p>
<p>&nbsp;效果图如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719085100567-881672799.png"></p>
<p>&nbsp;</p>
<p>(2)、CSS类绑定----添加或删除单个类的最佳途径</p>
<p>CSS类绑定的语法和property绑定类似。但是方括号中的部分不是元素的属性名,而是由class前缀,一个点(.)和css类的名字组成,其中后两部分是可选的,例如:。借助CSS类绑定,可以从元素的class attribute 上添加和移出css类名。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a、绑定语法1-----用yellow back 替换标准绑定(绑定多个css样式)</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;首先在组件中定义变量:&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> yellowBack = 'yellow back';</span>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;然后给&lt;span&gt;标签绑定上述定义的变量:&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;span class="red size back" ="yellowBack"&gt;css类绑定替换传统的样式绑定&lt;/span&gt;</span>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;通过[ class ]绑定的样式将替换class传统绑定的样式,效果图如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719090132249-1471752088.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; b、绑定语法2-----绑定单个css样式----绑定到特定的类名。当[ class.类名] = ‘true|false’,当为真的时候,Angular会添加这个类,否则会移出这个类</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 首先在组件中定义变量:&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> isRed = <span style="color: rgba(0, 0, 255, 1)">true</span>;</span>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 然后给&lt;span&gt;标签绑定上述定义的变量:&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;span ="isRed"&gt;css类绑定red类&lt;/span&gt;</span>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 效果图如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719090630365-1241248965.png"></p>
<p>&nbsp;</p>
<p>(3)、NgClass----同时添加或移出多个类</p>
<p>&nbsp; &nbsp; &nbsp; a、不简洁的写法(目的是为了加深对 的理解)</p>
<p>&nbsp; &nbsp; &nbsp; 首先在组件中定义几个变量:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(128, 0, 0, 1)">isRed = true;
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(128, 0, 0, 1)">isGreen = true;
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(128, 0, 0, 1)">isYellow = true;
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(128, 0, 0, 1)">isBack = true;
</span><span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(128, 0, 0, 1)">isSize: true;</span></pre>
</div>
<p>&nbsp; &nbsp; &nbsp;在模板页中写法如下:</p>
<p>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> &lt;span ="{'red':true,'back':true,'size':true}"&gt;第一种写法&lt;/span&gt;</span>&nbsp;</p>
<p>效果如下所示:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719095424886-608948139.png"></p>
<p>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="isRed?'red':''"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>第一种写法的另类风格<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></span>&nbsp;</p>
<p>效果如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719095519657-940100241.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; b、常规写法----将ngClass绑定到一个键值对对象,键为类名,值为bool类型,如果值为true,则对应的类被加上,否则该类会被移出。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 首先在组件中定义变量样式对象:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">currentClasses = {
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)">    'yellow': true,
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">    'back': false,
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 0, 1)">    'size': true
</span><span style="color: rgba(0, 128, 128, 1)">5</span>   };</pre>
</div>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 然后在模板页面对该样式对象进行绑定,根据值得true或者false决定对应样式是否会被添加。一般值会在组件中预先定义好,在修改的时候只需要修改预先定义好的变量即可</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="currentClasses"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>标准的ngClass<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>效果图如下所示:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719100422083-8388619.png"></p>
<p>&nbsp;</p>
<p>(4)、Style Binding----设置单一样式值的方法。样式绑定的语法与property属性绑定类似。但是方括号中的部分不是元素的属性名,而由style前缀、一个点(.)和css样式的属性名组成,形如:[ style.style-property]。有些样式绑定中的样式带有单位,这种情况,根据条件以‘em’和‘%’来设置字体大小的单位。</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="'red'"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Red<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="'green'"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Save<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="3"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Big<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">button </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="150"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Small<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>效果显示如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719103807682-1367861863.png"></p>
<p>注意:[ style.style-property]=“ ‘类名这里加单引号’ ”;style-property样式属性命名方法可以用中线命名法,就像这个例子中一样,也可以使用驼峰式命名法,比如fontSize.</p>
<p>&nbsp;</p>
<p>(5)、NgStyle----同时设置多个内联样式</p>
<p>&nbsp; &nbsp;首先在组件中定义变量和样式:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> isRed = <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">2</span>   curentStyle =<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">3</span>   'color': <span style="color: rgba(0, 0, 255, 1)">this</span>.isRed ? 'red' : 'green'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">4</span>   'border': '1px solid gray'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">5</span>   'fontSize': '20px'
<span style="color: rgba(0, 128, 128, 1)">6</span>   };</pre>
</div>
<p>&nbsp; &nbsp;在模板中将样式通过 [ ngStyle]&nbsp; 绑定</p>
<pre>&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">span </span><span style="color: rgba(255, 0, 0, 1)"></span><span style="color: rgba(0, 0, 255, 1)">="curentStyle"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>NgStyle Binding Test<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">span</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></span>&nbsp;<br>效果显示如下:</pre>
<p><img src="https://img2018.cnblogs.com/blog/1562037/201907/1562037-20190719111734061-701451943.png"></p>
<pre></pre>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    图片、jQuery资料下载地址:QQ群694406023(百度云资料太浪费内存了~~~~~~~~)
备注:一般代码中总会引入JQuery包,复制代码过程中请注意。用到的外部包文件群文件中都已上传。<br><br>
来源:https://www.cnblogs.com/WangYujie1994/p/11211222.html
頁: [1]
查看完整版本: Angular----样式