一.Vue2.5开发去哪儿网app首页②——iconfont的使用和代码优化

在iconfont官网下载所需的图标,将尾缀.svg  .ttf  .woff  .eot的字体文件放入styles中的iconfont文件夹中,将iconfont.css文件放入styles文件夹中

改下iconfont.css中文件加载路径

@font-face {font-family: "iconfont";
  src: url('./iconfont/iconfont.eot?t=1550063508301'); /* IE9 */
  src: url('./iconfont/iconfont.eot?t=1550063508301#iefix') format('embedded-opentype'), /* IE6-IE8 */
  url('data:application/x-font-woff2;) format('woff2'),
  url('./iconfont/iconfont.woff?t=1550063508301') format('woff'),
  url('./iconfont/iconfont.ttf?t=1550063508301') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
  url('./iconfont/iconfont.svg?t=1550063508301#iconfont') format('svg'); /* iOS 4.1- */
}

在main.js中引入iconfont.css文件

import './assets/styles/iconfont.css'

使用这些图标完成首页Header

<template>
  <div class="header">
    <div class="header-left">
      <div class="iconfont back-icon">&#xe624;</div>
    </div>
    <div class="header-input">
      <span class="iconfont">&#xe632;</span>
      输入城市/景点/游玩主题
    </div>
    <div class="header-right">
      城市
      <span class="iconfont arrow-icon">&#xe64a;</span>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HomeHeader'
}
</script>

<style lang="stylus" scoped>
  //1rem = html font-size = 50px
  //rem以字体大小为参考值,reset.css中字体大小为50px,设计图为2倍尺寸图,即86/100
  .header
    display: flex
    line-height: 0.86rem
    background: #00bcd4
    color: #fff
    .header-left
      width: .64rem
      float: left
      .back-icon
        font-size: 0.4rem
        text-align: center
    .header-input
      flex: 1
      height: 0.64rem
      line-height: 0.64rem
      margin-top: 0.12rem
      margin-left: 0.2rem
      padding-left: 0.2rem
      background: #fff
      border-radius: 0.1rem
      color: #ccc
    .header-right
      width: 1.24rem
      float: right
      text-align: center
      .arrow-icon
        margin-left: -0.04rem
        font-size:0.24rem

</style>

 Header中的背景色在其他地方也能用到,所以我们可以优化一下代码,让这个背景色可以各处去引用

在styles下创建一个varibles.styl文件,写入

$bgColor = #00bcd4

然后在Header组件的style中引入这个styl,在css中引入样式时需要在import前加@

@import'../../../assets/styles/varibles.styl'

@代表src目录,也可以使用@来引入,但在css中引入时需要在前面加~

@import'~@/assets/styles/varibles.styl'

这样,就可以在Header组件的style中使用 $bgColor这个变量了

 .header
    display: flex
    line-height: 0.86rem
    background: $bgColor
    color: #fff

styles目录也被用到很多次,我们可以简化styles目录,

在build中的webpack.base.conf.js中找到并添加一行代码即可

  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      'styles': resolve('src/assets/styles'),
    }
  },

这样就可以直接使用styles目录了 

@import'~styles/varibles.styl'

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风里有诗句哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值