angular5+ 如何點(diǎn)擊改變圖片顏色(png/svg)

二話不說(shuō),先上gif圖:

上png,下svg

綁定點(diǎn)擊事件 (click)="事件名()"
綁定src="{{變量名}}"
DOM元素獲?。篤iewChild, ElementRef, AfterViewInit;

ViewChild 是屬性裝飾器,用來(lái)從模板視圖中獲取匹配的元素。視圖查詢?cè)?ngAfterViewInit 鉤子函數(shù)調(diào)用前完成,因此在 ngAfterViewInit 鉤子函數(shù)中,才能正確獲取查詢的元素。

一、通過(guò)img的src實(shí)現(xiàn)圖片顏色切換

  • 綁定src :src="{{pngUrl}}"
  • 奇偶次切換,原理類似toggle
  <ul>
    <li>
//添加按鈕點(diǎn)擊事件pngChange
      <button type="button" name="button" (click)="pngChange()">藍(lán)色png,點(diǎn)擊變成綠色</button>
      <img width="40" alt="Angular Logo" src="{{pngUrl}}" #png class="png">
    </li>
</ul>
//DOM元素獲取的話需要import ViewChild, ElementRef, AfterViewInit;
import { Component,ViewChild,ElementRef, AfterViewInit } from '@angular/core';

export class AppComponent  {
  @ViewChild('png') png:ElementRef; //獲取到該元素

  public pngUrl = "../assets/blue.png"; //定義src的綁定變量
  public count1 = 0; //用以判斷點(diǎn)擊的奇偶次


 //點(diǎn)擊改變png圖片顏色,奇數(shù)綠色,偶數(shù)藍(lán)色,類似toggle
  pngChange(){
  this.count1 ++;
  if(this.count1 %2 ==1) {
    console.log(11);
    this.pngUrl="../assets/green.png";
    }else {
    this.pngUrl="../assets/blue.png";
          }
  }

//必須要寫的
ngAfterViewInit() {
     // console.log(222);
   }
}

二、<svg>直接嵌在html頁(yè)面中

<ul>
  <li>
    <button type="button" name="button" (click)="svgChange()">粉色svg,點(diǎn)擊點(diǎn)擊變成橙色</button>

 //path是矢量路徑,這個(gè)有專門的svg編輯器生成這些代碼,不用管~
       <svg  viewBox="0 0 1024 1024"  width="40" height="40" class="svg" #svg>
      <path d="M768 728.615385v-7.876923-11.815385c-11.815385-110.276923-122.092308-196.923077-256-196.923077s-244.184615 86.646154-256 192.984615v23.63077c0 43.323077 35.446154 78.769231 78.769231 78.76923h354.461538c43.323077 0 78.769231-35.446154 78.769231-78.76923zM512 1024C228.430769 1024 0 795.569231 0 512S228.430769 0 512 0s512 228.430769 512 512-228.430769 512-512 512z m0-555.323077c94.523077 0 169.353846-74.830769 169.353846-169.353846S606.523077 126.030769 512 126.030769s-169.353846 78.769231-169.353846 173.292308 74.830769 169.353846 169.353846 169.353846z" #path fill="#d4237a"></path>
    </svg>
  </li>
</ul>
//導(dǎo)包
import { Component,ViewChild,ElementRef, AfterViewInit } from '@angular/core';
//元素獲取 svg或者這個(gè)path它倆任意一個(gè)都能改變顏色,就是在事件觸發(fā)時(shí)找到nodeValue會(huì)有點(diǎn)差別~
@ViewChild('svg') svg:ElementRef;
@ViewChild('path') path:ElementRef;
//用以判斷奇偶次
public count2 = 0;
//事件觸發(fā)
  svgChange(){
    this.count2++;
    if(this.count2 % 2== 1) {
//針對(duì)svg的元素進(jìn)行變色屬性獲取操作,一般選這種方案,
  this.svg.nativeElement.firstChild.attributes[2].nodeValue="orange";
//這是對(duì)<path>標(biāo)簽的DOM獲取后的顏色操作
 // this.path.nativeElement.farthestViewportElement.firstChild.attributes.fill.nodeValue="orange";
    } else {      
  this.svg.nativeElement.firstChild.attributes[2].nodeValue="#d4237a";
// this.path.nativeElement.farthestViewportElement.firstChild.attributes.fill.nodeValue="#d4237a";
    }
  }
  • svg的屬性列表


    svg
this.svg.nativeElement.firstChild.attributes[2].nodeValue="你想要的顏色值"
  • path的屬性列表


    this.path.nativeElement.farthestViewportElement.firstChild.attributes.fill.nodeValue="你想要的顏色值"

其實(shí)都大同小異,獲取DOM以后console一下,控制臺(tái)里找一找,基本上關(guān)于屬性的改變大都記錄在attributes這一個(gè)關(guān)鍵key中~~

大愛(ài)svg,因?yàn)槭鞘噶繄D,一直保持清晰。咱們常用到的iconfont字體圖標(biāo),就是svg~svg不僅可以直接通過(guò)img的src引入,也可以把<svg>整個(gè)標(biāo)簽復(fù)制到html頁(yè)面中也沒(méi)有問(wèn)題~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一:什么是閉包?閉包的用處? (1)閉包就是能夠讀取其他函數(shù)內(nèi)部變量的函數(shù)。在本質(zhì)上,閉包就 是將函數(shù)內(nèi)部和函數(shù)外...
    xuguibin閱讀 10,018評(píng)論 1 52
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時(shí)...
    歐辰_OSR閱讀 30,194評(píng)論 8 265
  • F311+語(yǔ)淳+第十二次打卡,非暴力溝通訓(xùn)練營(yíng)第三期 最近發(fā)生的一次身邊案例 妹妹和我商量給老爸買一臺(tái)空氣凈化器,...
    語(yǔ)淳珺兒閱讀 472評(píng)論 2 0
  • 這周我們學(xué)習(xí)小組組織了"空巴",剛開(kāi)始我還不知道空巴是什么意思,現(xiàn)在終于明白原來(lái)空巴就是在輕松的氣氛中敞開(kāi)心扉交流...
    胡青青887782閱讀 243評(píng)論 0 0

友情鏈接更多精彩內(nèi)容