设计工具

画设计图工具 figma
https://www.figma.com/

按住Alt可以查看距离
获取网站图标: 域名+/favicon.ico

写html/css

复制淘宝的meta

防止双指缩放
image.png

根据HTML写好CSS选择器

image.png

parcel预览

  1. parcel src/index.html

css reset

  1. /* css reset */
  2. *{box-sizing: border-box;}
  3. *:before, *:after{box-sizing: border-box;}
  4. *{margin:0;padding:0;}
  5. ul, ol {list-style:none;}
  6. /* style */
  7. /* 给body加背景色,浏览器会自动给整个页面添加 */
  8. body {background: #eee;}

搜索框

  1. .globalHeader {
  2. border:1px solid red;
  3. margin:20px;
  4. /* 用flex布局使搜索框和按钮分列两边 */
  5. display: flex;
  6. justify-content:space-between;
  7. }
  8. .globalHeader > input {
  9. width: 100%;
  10. height: 40px;
  11. margin-right: 10px;
  12. padding: 0 10px;
  13. /* input有默认border,要覆盖掉或者为none */
  14. border:1px solid #ddd;
  15. border-radius: 4px;
  16. }
  17. .globalHeader > button {
  18. white-space: nowrap; /* 阻止换行 */
  19. border: none;
  20. border-radius: 4px;
  21. background: #2D9CDB;
  22. padding: 0 25px;
  23. font-size: 18px;
  24. color: white;
  25. }

用flex布局居中

  1. display: flex;
  2. justify-content: center;
  3. align-items: center;

换行

  1. flex-wrap: wrap;

添加icon

https://www.iconfont.cn/
image.png
image.png
image.png
image.png
image.png

实现百度搜索

  1. <form method="get" action="https://www.baidu.com/s" class="searchFrom" target="_blank">
  2. <input name="wd" type="text" />
  3. <button type="submit">搜索</button>
  4. </form>

实现点击跳转

用a标签包住div

  1. <a href="https://www.acfun.cn">
  2. <div class="site">
  3. <div class="logo">A</div>
  4. <div class="link">acfun.cn</div>
  5. </div>
  6. </a>

去除a标签默认样式

  1. a {color: inherit; text-decoration: none;}

显示logo

  1. <a href="https://www.bilibili.com">
  2. <div class="site">
  3. <div class="logo">
  4. <img class="bilibililogo"src="./images/bilibili.png" alt="bilibili">
  5. </div>
  6. <div class="link">bilibili.com</div>
  7. </div>
  8. </a>

去除img默认样式

  1. img {max-width: 100%; max-height: 100%;}

新增网站

引入jQuery

https://www.bootcdn.cn/
image.png
判断是否引入成功

  1. console.log(jQuery)
  2. console.log($)

监听新增按钮

  1. $('.addButton')
  2. .on('click', ()=>{
  3. window.prompt("请输入网址")
  4. })

手机调试

ipconfig查看电脑ip
手机浏览器输入ip:端口号

生成节点

$(“xxx”) 既是创建也是查找
用jQuery生成的节点变量名要以$开头

  1. $('.addButton')
  2. .on('click', ()=>{
  3. let url = window.prompt("请输入网址")
  4. if(url.indexOf("http")!==0){
  5. url = "https://" + url
  6. }
  7. console.log(url)
  8. // 创建li
  9. const $siteList = $(".siteList")
  10. const $lastLi = $siteList.find(".lastLi")
  11. const $li = $(`<li>
  12. <a href="${url}">
  13. <div class="site">
  14. <div class="logo">${url[0]}</div>
  15. <div class="link">${url}</div>
  16. </div>
  17. </a>
  18. </li>`).insertBefore($lastLi) // 链式操作
  19. })

用数组存储站点

  1. const weblist = [
  2. {logo:"A", logoType: "text", url: "https://www.acfun.cn"},
  3. {logo:"./images/bilibili.png", logoType: "image", url: "https://www.bilibili.com"}
  4. ]
  5. const $siteList = $(".siteList")
  6. const $lastLi = $siteList.find(".lastLi")
  7. const render = () => {
  8. $siteList.find('li:not(.lastLi)').remove() //清除现有li, .lastLi除外
  9. // 遍历数组,重新渲染每个节点
  10. weblist.forEach(node => {
  11. const $li = $(`<li>
  12. <a href="${node.url}">
  13. <div class="site">
  14. <div class="logo">${node.logo[0]}</div>
  15. <div class="link">${node.url}</div>
  16. </div>
  17. </a>
  18. </li>`).insertBefore($lastLi)
  19. })
  20. }
  21. render()
  22. $('.addButton')
  23. .on('click', ()=>{
  24. let url = window.prompt("请输入网址")
  25. if(url.indexOf("http")!==0){
  26. url = "https://" + url
  27. }
  28. weblist.push({logo:url[0], logoType: "text", url:url})
  29. render()
  30. })

页面关闭时调用

  1. window.onbeforeunload = ()=>{
  2. console.log("要关闭了")
  3. }

image.png

存入localStorage

localStorage只接受字符串

  1. window.onbeforeunload = ()=>{
  2. const string = JSON.stringify(weblist) // 转为字符串
  3. window.localStorage.setItem("x", string) // 2个参数,一个key, 一个value
  4. }

image.png

读取localStorage

  1. const x = localStorage.getItem("x") // 获取到的是字符串
  2. const xObject = JSON.parse(x) // 解析成对象
  3. const weblist = xObject || [
  4. {logo:"A", logoType: "text", url: "https://www.acfun.cn"},
  5. {logo:"./images/bilibili.png",
  6. logoType: "image", url: "https://www.bilibili.com"}
  7. ]

清除localStorage

image.png

2句话

  1. 顶一个小目标,实现它
  2. 没有完美,做到看不出bug

    去除网址前缀

    replace() 方法返回一个替换后新字符串。原字符串不会改变。

    1. const simplify = (url) => {
    2. return url.replace("https://","")
    3. .replace("http://","")
    4. .replace("www.","")
    5. .replace(/\/.*/,'') // 删除/开头的内容
    6. }

    小写变大写

    JS

    1. .toUpperCase()

    CSS

    1. text-transform: uppercase;

    添加删除图标

    1. weblist.forEach(node => {
    2. const $li = $(`<li>
    3. <div class="site">
    4. <div class="logo">${node.logo[0]}</div>
    5. <div class="link">${simplify(node.url)}</div>
    6. <div class="close">
    7. <svg class="icon" >
    8. <use xlink:href="#icon-delete"></use>
    9. </svg>
    10. </div>
    11. </div>
    12. </li>`).insertBefore($lastLi)
    13. $li.on('click',()=>{
    14. window.open(node.url, "_self") // 打开网址, _self当前窗口
    15. })
    16. $li.on('click', '.close', (e)=>{
    17. e.stopPropagation() // 阻止冒泡
    18. })
    19. })

    定位 ```css .siteList > li { margin-bottom: 10px; position: relative; }

.siteList .site .close { position: absolute; top: 3px; right: 5px; }

  1. <a name="El7SP"></a>
  2. ### 实现删除
  3. forEach可以传2个参数,第2个参数是索引<br />还可以传第3个参数,当前数组
  4. ```javascript
  5. weblist.forEach((node,index) => { // 传index
  6. const $li = $(`<li>
  7. <div class="site">
  8. <div class="logo">${node.logo[0]}</div>
  9. <div class="link">${simplify(node.url)}</div>
  10. <div class="close">
  11. <svg class="icon" >
  12. <use xlink:href="#icon-delete"></use>
  13. </svg>
  14. </div>
  15. </div>
  16. </li>`).insertBefore($lastLi)
  17. $li.on('click',()=>{
  18. window.open(node.url, "_self") // 打开网址, _self当前窗口
  19. })
  20. $li.on('click', '.close', (e)=>{
  21. e.stopPropagation() // 阻止冒泡
  22. weblist.splice(index,1) // 删除
  23. render()
  24. })
  25. })

媒体查询

将媒体查询写在最后面

  1. @media (min-width:500px) {
  2. .searchFrom {
  3. max-width: 400px;
  4. margin-left: auto;
  5. margin-right: auto;
  6. }
  7. }

负margin平均布局

  1. .parent {
  2. display: flex;
  3. flex-wrap: wrap;
  4. margin-right: -25px;
  5. }

justify-content: flex-start; / 覆盖space-between /

鼠标进入时显示

  1. .siteList .site .close {
  2. display: none;
  3. }
  4. .siteList .site:hover .close {
  5. display: block;
  6. }

鼠标样式

  1. cursor: pointer; /* 手形 */
  2. cursor: default; /* 默认 */

监听键盘

  1. document.addEventListener('keypress', (e)=>{
  2. const {key} = e
  3. for (let node of weblist){
  4. if (node.logo.toLowerCase() === key) {
  5. window.open(node.url, "_self")
  6. }
  7. }
  8. })

添加背景

  1. body {
  2. background: #eee url(./images/bg.jpg) no-repeat center center;
  3. background-size: cover; /* 填充 */
  4. height: 100vh;
  5. }
  6. .wrapper {
  7. overflow:hidden; // 阻止margin合并
  8. }

发布到github

  1. rm -rf dist # 删掉dist文件夹
  2. parcel build src/index.html --no-minify # 不压缩,应对svg报错

image.png
打开build好的文件发现地址有问题
parcel build —help 查看帮助

  1. parcel build src/index.html --no-minify --public-url .
  2. # 或者
  3. parcel build src/index.html --no-minify --public-url ./

新建.gitignore文件
image.png
上传到github

yarn一键build

  1. yarn init -y # 生成package.json文件

修改package.json文件,添加一行

  1. "scripts": {
  2. "build": "rm -rf dist && parcel build src/index.html --no-minify --public-url ./"
  3. },

build

  1. yarn build