HTML入門——互動式推送初嘗試

0.背景

  疫情原因,導致許多大眾喜聞樂見的體育活動停擺,但部落客和隊友們運營的體育社團公眾號不能停擺。為了利用當下線上活動頻率高的契機增加關注量,加之微信推送的互動性已成為趨勢,部落客打算和隊友們嘗試實現互動式推送,在此分享一些自己整理的經驗。不奢求達到「塗小瓶子」「點亮武漢」等活動的風靡程度,但求所有努力能得到用戶滴認可。

1.HTML格式簡介

  超文本標記語言(英語:HyperText Markup Language,簡稱:HTML)是一種用於創建網頁的標準標記語言。參考來源:菜鳥教程網站。

  HTML文件由文件頭、主體等部分組成,最大的特點是標籤化管理。

  注意,一個標籤(例如a標籤)的開始與結尾分別用<a></a>表示,也可以表示為<a />,部分不規範的HTML文檔會省略後面的</a>。
  標籤的內容放在兩個尖括弧之間,標籤的屬性放在尖括弧"里",像這樣:<style type="text/css">,表示style標籤的type屬性為"text或者css"
  (註:CSS是層疊樣式表(Cascading Style Sheets),以下是從菜鳥教程網站上搬過來的CSS簡單介紹

  • 樣式定義如何顯示 HTML 元素
  • 樣式通常存儲在樣式表中
  • 把樣式添加到 HTML 4.0 中,是為了解決內容與表現分離的問題
  • 外部樣式表可以極大提高工作效率
  • 外部樣式表通常存儲在 CSS 文件中
  • 多個樣式定義可層疊為一個

  此處不必細究,會套用示例就行。

2.示例

<!DOCTYPE html>
<html>
<head>
    <meta charset="gbk">
    <style type="text/css">
        body {background-color:white}
        p {color:purple}
    </style>
</head>

   這是示例的HTML文件頭部,下面進入主體部分。
  此處參考文章為:網易王三三的《
絕對色感の挑戰:不 瞎 算 你 贏》 ,示例的大部分程式碼來自於該文章,僅做學慣用途。

2.1 插入文字

p標籤代表一段話(paragraph),可以設置風格樣式、頁面位置等,我們要顯示的文字在這裡放在<span>標籤下,大家把「請看題:以下哪個是」用需要顯示的內容替換即可。

另外,我們注意到下面程式碼第8行的span標籤多了一個color: rgb(204, 0, 0);的color屬性,說明對這半句話「中國乒乓球隊隊服的主流顏色」的字體顏色進行了設置,為了突出顯示。顏色就是RGB格式的(204, 0, 0),如果第一個參數==255且後兩個參數為0,即為純正的紅色。

<body>
<p style="max-width: 100%;min-height: 1em;white-space: normal;margin-left: 16px;margin-right: 16px;line-height: 1.75em;letter-spacing: 0.5px;font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;">
    <span style="font-size: 15px;">
        <span style="font-size: 15px;text-align: left;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">&nbsp;請看題:以下哪個是
        </span>
        <span style="font-size: 15px;text-align: left;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;box-sizing: border-box;color: rgb(204, 0, 0);">中國乒乓球隊隊服的主流顏色
        </span>
        <span style="font-size: 15px;text-align: left;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">?
        </span>
    </span>
    <br  />
</p>

2.2 插入圖片

以下的內容由多個section標籤嵌套構成,我們只需要替換下面2.3節第二個程式碼框中第5行的background-image後面跟著的url即可。

需要注意的是,替換時只需要更改中間的圖片鏈接,開始處的 (&quot; 和結尾的 &quot;) 都不需要動。

url(&quot;//baike.so.com/gallery/listghid=first&pic_idx=1&eid=1395020&sid=1474879&quot;)

圖片鏈接可以選擇網路圖片-右鍵-複製鏈接地址,也可以先從本地上傳圖片到網路(比如傳到私人部落格或BBS),只要能保證圖片鏈接在使用期內不會丟失即可。(靠譜的網站不會倒閉,不要選擇新 浪 微 博 相 冊這種會下線的功能就行)

2.3添加 點擊-切換到背景圖片 動畫

  生成動畫的標籤為svg,用svg繪圖的文章一搜一大把,在此不贅述了。svg下面的具體動畫動作標籤為animate,以下面的動畫標籤為例:簡便起見,我們只關注fill、from、to、duration、begin標籤。

  • fill="freeze"代表動畫效果展示完畢後保留,可選相對應的屬性為"remove",代表完成後去除動畫效果。
  • from和to代表狀態的變化,"0"代表透明,"1"代表完全不透明,從1變為0即為「圖片消失」效果,反之,「圖片出現」。btw, 其他標籤可能有的屬性opacity也是透明度的意思。
  • begin代表動畫觸發機制,可選值為點擊"click"、點擊+滯後時間"click+0.1s"、打開頁面後一定時間內自動觸發"2000ms"、"0.16s"等。
  • duration代表動畫完成需要的時長。
<animate attributename="width" style="box-sizing: border-box;" fill="freeze" to="0" from="1" duration="0.01" begin="click + 0.1s"
</animate>

  以下是一個完整section的內容,包含了圖片顯示的box位置、大小,圖片鏈接,動畫效果。

  要實現的效果是,打開頁面時展示紅色圖片,點擊圖片後顯示張繼科身著西紅柿炒雞蛋色球服圖片。

  紅色圖片不是插入的外部圖片,可繪製紅色填充矩形得到,搜索<rec>標籤即可。

  紅色圖片的動畫是「點擊-立刻從1不透明到0透明」,點擊後,接著會觸發背景圖片(科科+球服圖片)的動畫「點擊-0.1s後-從0透明到1不透明」。

<section style="box-sizing: border-box;font-size: 16px;">
   ……
                <section style="background-image: url(&quot;//baike.so.com/gallery/list?ghid=first&pic_idx=1&eid=1395020&sid=1474879&quot;);background-position: 50% 50%;background-repeat: no-repeat;background-size: cover;background-attachment: scroll;width: 95%;margin-left: auto;margin-right: auto;box-sizing: border-box;"><section class="group-empty" style="display: inline-block;width: 100%;height: 105px;vertical-align: top;overflow-y: auto;border-style: none;border-width: 0px;border-radius: 10px;border-color: rgb(62, 62, 62);box-sizing: border-box;-webkit-overflow-scrolling: touch;"></section></section><section style="box-sizing: border-box;margin-top: -105px;height: 105px;">
                    <section class="top-div" style="height: 100%;box-sizing: border-box;">
                        <svg class="svg-layout-content root-svg" width="95%" height="100%" xmlns="//www.w3.org/2000/svg" style="box-sizing: border-box;transform: rotateZ(0deg);-webkit-transform: rotateZ(0deg);-moz-transform: rotateZ(0deg);-o-transform: rotateZ(0deg);width: 95%;" opacity="1">
                            <animate class="init-width-animation" attributename="width" style="box-sizing: border-box;" fill="freeze" to="95.00%" from="95.00%" begin="0s" duration="0.01">
                            </animate>
                            <animate attributename="width" style="box-sizing: border-box;" fill="freeze" to="0" from="1" duration="0.01" begin="click + 0.1s">
                            </animate>
                  ……
</section>

 

3.示例的完整程式碼

注意:微信公眾號編輯器無法直接對HTML程式碼進行轉換,可以先在其他編輯器中點擊html轉換按鈕轉換後,選擇複製正文(而不是編輯框中的程式碼)粘貼到微信公眾號編輯器中。

以135編輯器//www.135editor.com/為例,先點擊HTML轉換按鈕,再選擇右側-微信複製即可。另外可點擊右側-手機預覽,在手機端掃描臨時鏈接查看效果。如下圖所示:

 

示例的完整程式碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="gbk">
    <style type="text/css">
        body {background-color:white}
        p {color:purple}
    </style>
</head>

<body> <p style="max-width: 100%;min-height: 1em;white-space: normal;margin-left: 16px;margin-right: 16px;line-height: 1.75em;letter-spacing: 0.5px;font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;box-sizing: border-box !important;overflow-wrap: break-word !important;"> <span style="font-size: 15px;"> <span style="font-size: 15px;text-align: left;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">&nbsp;請看題:以下哪個是 </span> <span style="font-size: 15px;text-align: left;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;box-sizing: border-box;color: rgb(204, 0, 0);">中國乒乓球隊隊服的主流顏色 </span> <span style="font-size: 15px;text-align: left;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, &quot;Helvetica Neue&quot;, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">? </span> </span> <br /> </p> <section style="box-sizing: border-box;font-size: 16px;"> <section style="text-align: center;margin-top: 10px;margin-right: 0%;margin-left: 0%;box-sizing: border-box;" powered-by="xiumi.us"> <section style="display: inline-block;vertical-align: top;width: 33.33%;padding: 3px;box-sizing: border-box;"> <section style="box-sizing: border-box;" powered-by="xiumi.us"> <section style="background-image: url(&quot;//baike.so.com/gallery/list?ghid=first&pic_idx=1&eid=1395020&sid=1474879&quot;);background-position: 50% 50%;background-repeat: no-repeat;background-size: cover;background-attachment: scroll;width: 95%;margin-left: auto;margin-right: auto;box-sizing: border-box;"><section class="group-empty" style="display: inline-block;width: 100%;height: 105px;vertical-align: top;overflow-y: auto;border-style: none;border-width: 0px;border-radius: 10px;border-color: rgb(62, 62, 62);box-sizing: border-box;-webkit-overflow-scrolling: touch;"></section></section><section style="box-sizing: border-box;margin-top: -105px;height: 105px;"> <section class="top-div" style="height: 100%;box-sizing: border-box;"> <svg class="svg-layout-content root-svg" width="95%" height="100%" xmlns="//www.w3.org/2000/svg" style="box-sizing: border-box;transform: rotateZ(0deg);-webkit-transform: rotateZ(0deg);-moz-transform: rotateZ(0deg);-o-transform: rotateZ(0deg);width: 95%;" opacity="1"> <animate class="init-width-animation" attributename="width" style="box-sizing: border-box;" fill="freeze" to="95.00%" from="95.00%" begin="0s" duration="0.01"> </animate> <animate attributename="width" style="box-sizing: border-box;" fill="freeze" to="0" from="1" duration="0.01" begin="click + 0.1s"> </animate> <svg xmlns="//www.w3.org/2000/svg" width="100%" height="100%" style="box-sizing: border-box;"> <svg class="rect-content" width="100%" height="100%" style="box-sizing: border-box;"> <rect width="100%" height="100%" style="box-sizing: border-box;opacity: 1;fill: rgb(255, 0, 0);" x="0%"> </rect> </svg> <svg style="width: 100%;height: 100%;box-sizing: border-box;" version="1.1" xmlns="//www.w3.org/2000/svg"> </svg> </svg> <animate attributename="opacity" style="box-sizing: border-box;" fill="freeze" dur="0.1" begin="click" from="1" to="0"> </animate> </svg> </section> </section> </section> </section> <section style="display: inline-block;vertical-align: top;width: 33.33%;padding: 3px;box-sizing: border-box;"> <section style="box-sizing: border-box;" powered-by="xiumi.us"> <section class="group-empty" style="display: inline-block;width: 95%;height: 105px;vertical-align: top;overflow-y: auto;border-width: 0px;border-radius: 10px;border-style: none;border-color: rgb(62, 62, 62);box-sizing: border-box;-webkit-overflow-scrolling: touch;"> </section> <section style="box-sizing: border-box;margin-top: -105px;height: 105px;"> <section class="top-div" style="height: 100%;box-sizing: border-box;"> <svg class="svg-layout-content root-svg" width="95%" height="100%" xmlns="//www.w3.org/2000/svg" style="box-sizing: border-box;transform: rotateZ(0deg);-webkit-transform: rotateZ(0deg);-moz-transform: rotateZ(0deg);-o-transform: rotateZ(0deg);width: 95%;" opacity="1"> <animate class="init-width-animation" attributename="width" style="box-sizing: border-box;" fill="freeze" to="95.00%" from="95.00%" begin="0s" duration="0.01"> </animate> <animate attributename="width" style="box-sizing: border-box;" fill="freeze" to="0" from="1" duration="0.01" begin="click + 0.1s"> </animate> <svg xmlns="//www.w3.org/2000/svg" width="100%" height="100%" style="box-sizing: border-box;"> <svg class="rect-content" width="100%" height="100%" style="box-sizing: border-box;"> <rect width="100%" height="100%" style="box-sizing: border-box;opacity: 1;fill: rgb(0, 0, 255);" x="0%"> </rect> </svg> <svg style="width: 100%;height: 100%;box-sizing: border-box;" version="1.1" xmlns="//www.w3.org/2000/svg"> </svg> </svg> <animate attributename="opacity" style="box-sizing: border-box;" fill="freeze" dur="0.1" begin="click" from="1" to="0"> </animate> </svg> </section> </section> </section> </section> <section style="display: inline-block;vertical-align: top;width: 33.33%;padding: 3px;box-sizing: border-box;"> <section style="box-sizing: border-box;" powered-by="xiumi.us"> <section class="group-empty" style="display: inline-block;width: 95%;height: 105px;vertical-align: top;overflow-y: auto;border-width: 0px;border-radius: 10px;border-style: none;border-color: rgb(62, 62, 62);box-sizing: border-box;-webkit-overflow-scrolling: touch;"> </section> <section style="box-sizing: border-box;margin-top: -105px;height: 105px;"> <section class="top-div" style="height: 100%;box-sizing: border-box;"> <svg class="svg-layout-content root-svg" width="95%" height="100%" xmlns="//www.w3.org/2000/svg" style="box-sizing: border-box;transform: rotateZ(0deg);-webkit-transform: rotateZ(0deg);-moz-transform: rotateZ(0deg);-o-transform: rotateZ(0deg);width: 95%;" opacity="1"> <animate class="init-width-animation" attributename="width" style="box-sizing: border-box;" fill="freeze" to="95.00%" from="95.00%" begin="0s" duration="0.01"> </animate> <animate attributename="width" style="box-sizing: border-box;" fill="freeze" to="0" from="1" duration="0.01" begin="click + 0.1s"> </animate> <svg xmlns="//www.w3.org/2000/svg" width="100%" height="100%" style="box-sizing: border-box;"> <svg class="rect-content" width="100%" height="100%" style="box-sizing: border-box;"> <rect width="100%" height="100%" style="box-sizing: border-box;opacity: 1;fill: rgb(255, 0, 255);" x="0%"> </rect> </svg> <svg style="width: 100%;height: 100%;box-sizing: border-box;" version="1.1" xmlns="//www.w3.org/2000/svg"> </svg> </svg> <animate attributename="opacity" style="box-sizing: border-box;" fill="freeze" dur="0.1" begin="click" from="1" to="0"> </animate> </svg> </section> </section> </section> </section> </section> <section style="margin-top: 10px;box-sizing: border-box;" powered-by="xiumi.us"> <section style="color: rgb(165, 165, 165);box-sizing: border-box;"> <p style="text-align: center;white-space: normal;box-sizing: border-box;">歡迎關注xx乒協! <br style="box-sizing: border-box;" />▼ </p> </section> </section> </body

 

後記——

  寫這篇部落格前,部落客曾糾結於這份本應貢獻給社團宣傳部的內部資料是否應該公之於眾,但最終認為開源有助於成長,況且源程式碼+技術資料本是公開的,應多多分享交流。部落客作為前端小白(甚至可以說是外行人都不為過)前後利用約5-6個小時搜集整理了相關資料,希望努力沒有白費,也但願逐夢之路能夠堅持,立此為證。

  五一勞動節,要更加努力啊!

 

Tags: