地址栏文字超出显示三个点

image.png

  1. .address {
  2. white-space: nowrap;
  3. overflow: hidden;
  4. text-overflow: ellipsis;
  5. }

scss技巧

scss变量都放在helpers.scss文件中

  1. $content-font-color: #333;

将 ellipsis 封装成 scss 的 placeholder

  1. %ellipsis {
  2. white-space: nowrap;
  3. overflow: hidden;
  4. text-overflow: ellipsis;
  5. }

使用

  1. @import './style/helpers.scss';
  2. .address {
  3. @extend %ellipsis;
  4. }

也可以封装成 mixin

  1. @mixin ellipsis {
  2. white-space: nowrap;
  3. overflow: hidden;
  4. text-overflow: ellipsis;
  5. }

使用

  1. .address {
  2. @include ellipsis;
  3. }

placeholder 和 mixin 的区别

The difference between mixins and placeholders is that placeholders consolidates mutually-shared code, whereas mixins just assign the properties to the individual classes — along with whatever was specific to that class. Because of this, it’s preferential to use placeholders when possible, and it’s not always possible. As mentioned earlier, placeholders aren’t able to take parameters, making them less useful when code is dependent upon variables. It’s better to use mixins in those cases.

搜索栏

input设置placeholder颜色

  1. > input {
  2. background: none;
  3. border: none;
  4. height: 100%;
  5. outline: none;
  6. &::placeholder {
  7. color: $grey-font;
  8. }
  9. }

deep伪类

在vue中,style加上scoped,则只会作用在当前组件的元素上,如果要作用在其他组件内部,需要使用deep伪类

  1. <style scoped>
  2. .a :deep(.b) {
  3. /* ... */
  4. }
  5. </style>

banner

解决图片加载时抖动问题:让父元素先占据图片需要的位置,这样它后面的元素就不会跳动

  1. .banner {
  2. margin-top: 12px;
  3. height: 0;
  4. overflow: hidden;
  5. padding-bottom: 86px;
  6. > img {
  7. width: 100%;
  8. }
  9. }

给图片设置 height 也可以

gap

image.png
父元素有左右padding, 要让这个gap占据100%的父元素宽度,只需左右margin设为负值

  1. .gap {
  2. height: 10px;
  3. background: #f1f1f1;
  4. margin: 0 -18px;
  5. }

name的作用

chrome安装vue devtools插件
vue devtools根据vue文件中的name属性读取组件名字,如果没有name, 则将vue文件名作为组件名

  1. <script lang="ts">
  2. export default {
  3. name: 'Home',
  4. }
  5. </script>

动态加载图片

在vue中如果图片的src绑定本地路径,wepback会直接解析为字符串,导致无法加载图片,需要使用require动态加载

  1. // 无法加载
  2. <div class="shop-img">
  3. <img :src="../assets/images/wowmall.png" alt="">
  4. </div>
  1. // 使用require动态加载
  2. <div class="shop-img">
  3. <img :src="imgUrl" alt="">
  4. </div>
  5. <script>
  6. // ...
  7. const imgUrl = require('../assets/images/wowmall.png')
  8. // ...
  9. </script>

让图片有圆角

方法一: 直接设置图片的 border-radius

  1. .banner {
  2. padding: 0 18px;
  3. img {
  4. border-radius: 5px;
  5. width: 100%;
  6. height: 86px;
  7. object-fit: cover;
  8. object-position:center 25%;
  9. }
  10. }

方法二: 将图片设置为 div 的 background

  1. .banner {
  2. height: 86px;
  3. border-radius: 5px;
  4. background-image: url('https://s6.jpg.cm/2022/01/03/LtOxlW.jpg');
  5. background-size: cover;
  6. background-repeat: no-repeat;
  7. margin: 0 18px;
  8. }

父元素固定,子元素滚动

父元素设置 overflow: hidden;
子元素设置 overflow-y: scroll;

让图片覆盖父元素

  1. .papa {
  2. width: 68px;
  3. height: 68px;
  4. background: red;
  5. > img {
  6. width: 100%;
  7. height: 100%;
  8. object-fit: cover;
  9. }