【Vue】图片拉近、全屏背景实战经验总结

1 图片拉近
缘起是看到了下面的图片,我发现当鼠标悬浮的时候,发现他是可以拉近的,也就是图片的宽高不变,但是图片被放大了

起初我以为是有一个这样的方法,可以实现这个操作,但是查找了很多网站都没有找到。
后来自己慢慢的摸索,才了解了其中的奥妙
子盒子和父盒子的大小一样大,为了能体现出父子盒子,我让父盒子略大,并未黄色,子盒子为白色

当鼠标悬浮到该子盒子的时候,子盒子被放大(scale),如下图

同时,我设置子盒子超出父盒子的部分隐藏(overflow:hidden),这样我只能看到黄色部分,但是图片却被放大了,就看到了最初的效果,图片好像被拉进了

这样似乎太突兀了,我们加一个实现的时间 1s transition: 1s;

这样就舒服多了
写这一篇博客,我发现这个图片功能太简单了。我自己用 vue 做的时候,用的是 @mouseover @mouseout 需要两个方法
让其有 1s 动画效果的时候,我用的是根据类名获取元素,然后鼠标悬浮的时候给他加上动画的效果。
但是动画的效果默认是不会保存的,间隔一秒后又会回到初始化的状态,所以又加了一句 animation-fill-mode:forwards,让其保持动画的状态。
写这篇博客之后,原来只需要几行代码就解决了!
2 全屏背景

2.1 background-attachment
background-attachment 设置背景图像是否固定或者随着页面的其余部分滚动。
scroll 就是说,这个背景图片不是固定的,会随着页面高度的改变而变
fixed 意味着,图片是固定的,不会随着页面高度的改变而改变
2.2 background-size
2.3 display:flex;
弹性盒子由弹性容器(Flex container)和弹性子元素(Flex item)组成。
弹性容器通过设置 display 属性的值为 flex 或 inline-flex 将其定义为弹性容器。
弹性容器内包含了一个或多个弹性子元素。
不加 display:flex

加上 display:flex

还是挺有用的以前一直用,float:left,float:right 解决布局,现在又学到了一个方法。
2.4 flex-direction
flex-direction 属性规定灵活项目的方向。
**注意:**如果元素不是弹性盒对象的元素,则 flex-direction 属性不起作用。
本来是按行排序的,flex-direction 可以灵活的排序,可以按列排序,也可以按行反着来一些效果如下:


2.5 100vh
vh 就是当前屏幕可见高度的 1%,也就是说
height:100vh == height:100%;
但是当元素没有内容时候,设置 height:100%,该元素不会被撑开,此时高度为 0,
但是设置 height:100vh,该元素会被撑开与屏幕高度一致。
对比
100%

100vh

版权声明: 本文为 InfoQ 作者【游坦之】的原创文章。
原文链接:【http://xie.infoq.cn/article/afc848e131980b870a1ad3ad9】。文章转载请联系作者。
评论