div背景 css网格背景

View demo 查看演示Download Source 下载源

If you follow our UI Interactions & Animations Roundups, you might have spotted this beautiful grid designed by the folks of tubik:

如果您遵循我们的UI交互和动画摘要,则可能已经发现了tubik员工设计的这个漂亮的网格:

演示地址

Previously, Zhenya Rynzhuk also designed this wonderful layout with a similar interaction:

之前, Zhenya Rynzhuk还设计了这种具有相似交互作用的出色布局:

演示地址

It’s not too complicated to implement this. I wanted to try it and in the following I’ll walk you through the relevant markup and code.

实现它并不太复杂。 我想尝试一下,下面我将引导您完成相关的标记和代码。

网格的标记和样式 (The markup and style for the grid)

The markup is simply a grid of items that have background images. I like to use this structure because it allows me to control the sizes of the images by setting their position in the grid.

标记只是具有背景图像的项目的网格。 我喜欢使用这种结构,因为它允许我通过设置图像在网格中的位置来控制图像的大小。

<div class="grid"><div class="grid__item pos-1"><div class="grid__item-img" style="background-image:url(img/1.jpg);"></div></div><div class="grid__item pos-2"><div class="grid__item-img" style="background-image:url(img/2.jpg);"></div></div><div class="grid__item pos-3"><div class="grid__item-img" style="background-image:url(img/3.jpg);"></div></div>...
</div>

The grid is stretched to be a bit bigger than its parent because we want to move the items and create the illusion of an infinite plane of images.

网格被拉伸到比其父网格大一点,因为我们要移动项目并创建无限的图像平面的错觉。

.grid {pointer-events: none;position: absolute;width: 110%;height: 110%;top: -5%;left: -5%;display: grid;grid-template-columns: repeat(50,2%);grid-template-rows: repeat(50,2%);
}.grid__item {position: relative;
}.grid__item-img {position: relative;width: 100%;height: 100%;background-size: cover;background-position: 50% 50%;
}

The grid is divided into 50 cells for the rows and columns. With this layout density, the position of each image element can be set precisely.

网格分为行和列的50个单元格。 利用这种布局密度,可以精确设置每个图像元素的位置。

/* Shorthand grid-area: grid-row-start / grid-column-start / grid-row-end / grid-column-end */.pos-1 {grid-area: 10 / 1 / 26 / 7;
}.pos-2 {grid-area: 1 / 18 / 9 / 27;
}.pos-3 {grid-area: 1 / 36 / 14 / 42;
}...

Note that I use the double division structure for the possibility of moving the inner element with the background image to create the motion effect seen in demo 3. For that case, I define some extra styles:

请注意,我使用双分割结构来将内部元素与背景图像一起移动,以创建演示3中看到的运动效果。在这种情况下,我定义了一些额外的样式:

/* If we want to move the inner image */.grid--img .grid__item {overflow: hidden;display: flex;align-items: center;justify-content: center;will-change: transform;
}.grid--img .grid__item-img {flex: none;width: calc(100% + 100px);height: calc(100% + 100px);will-change: transform;
}

JavaScript (The JavaScript)

Now, let’s have a look at the JavaScript part. I’m using GSAP by GreenSock. We start by creating a Grid class to represent the grid of pictures:

现在,让我们看一下JavaScript部分。 我正在使用GreenSock的GSAP 。 我们首先创建一个Grid类来表示图片的网格:

export default class Grid {constructor(el) {this.DOM = {el: el};this.gridItems = [];this.items = [...this.DOM.el.querySelectorAll('.grid__item')];this.items.forEach(item => this.gridItems.push(new GridItem(item)));this.showItems();}...
}const grid = new Grid(document.querySelector('.grid'));

There should be an initial animation where the grid items scale up and fade in. We can add a method to the class for that. We also want the items to start at different times and for that we use the GSAP stagger option. The items will start animating from the center of the grid:

应该有一个初始动画,其中网格项会按比例放大和淡入。我们可以为此添加一个方法。 我们还希望项目在不同的时间开始,为此,我们使用GSAP stagger选项。 这些项目将从网格中心开始动画:

showItems() {gsap.timeline().set(this.items, {scale: 0.7, opacity: 0}, 0).to(this.items, {duration: 2,ease: 'Expo.easeOut',scale: 1,stagger: {amount: 0.6, grid: 'auto', from: 'center'}}, 0).to(this.items, {duration: 3,ease: 'Power1.easeOut',opacity: 0.4,stagger: {amount: 0.6, grid: 'auto', from: 'center'}}, 0);
}

Now, let’s make the items move as we move the mouse around. Each grid item will be represented by a GridItem class:

现在,让我们随着鼠标的移动来移动项目。 每个网格项将由一个GridItem类表示:

class GridItem {constructor(el) {this.DOM = {el: el};this.move();}...
}

The position of each item in both axes should be mapped with the mouse position. So, the mouse can move from position 0 to the width or height of the window. As for the item, it’ll move in a range of [start, end] that we need to specify. We’ll be assigning random values for the start/end value so that each item moves differently from each other.

每个项目在两个轴上的位置都应与鼠标位置对应。 因此,鼠标可以从位置0移动到窗口的宽度或高度。 至于项目,它将在我们需要指定的[开始,结束]范围内移动。 我们将为开始/结束值分配随机值,以使每个项目的移动方式彼此不同。

Let’s add the move method to the GridItem class:

让我们将move方法添加到GridItem类中:

move() {// amount to move in each axislet translationVals = {tx: 0, ty: 0};// get random start and end movement boundariesconst xstart = getRandomNumber(15,60);const ystart = getRandomNumber(15,60);// infinite loopconst render = () => {// Calculate the amount to move.// Using linear interpolation to smooth things out.// Translation values will be in the range of [-start, start] for a cursor movement from 0 to the window's width/heighttranslationVals.tx = lerp(translationVals.tx, map(mousepos.x, 0, winsize.width, -xstart, xstart), 0.07);translationVals.ty = lerp(translationVals.ty, map(mousepos.y, 0, winsize.height, -ystart, ystart), 0.07);gsap.set(this.DOM.el, {x: translationVals.tx, y: translationVals.ty});  requestAnimationFrame(render);}requestAnimationFrame(render);
}

And that’s it!

就是这样!

I hope you find this helpful and please let me know your feedback via @codrops. Thank you for reading!

希望对您有所帮助,并通过@codrops告诉我您的反馈意见。 感谢您的阅读!

翻译自: https://tympanus.net/codrops/2020/06/10/how-to-create-a-motion-hover-effect-for-a-background-image-grid/

div背景 css网格背景

div背景 css网格背景_如何为背景图像网格创建运动悬停效果相关推荐

  1. html内容被背景图片遮住怎么办_定义BODY背景图后,DIV的背景图片显示不全(已解决)...

    你的位置: 问答吧 -> WEB标准 -> 问题详情 定义BODY背景图后,DIV的背景图片显示不全(已解决) 一个有趣的问题,当BODY有背景图片的时候,BODY里的DIV一旦也有背景图 ...

  2. 网页背景 css 黑色,这段让网页背景变成纯黑色的浏览器插件如何优化?

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 手机via浏览器可以实现,但是他的代码有个问题就是网页加载的时候还是先显示全部白色再显示黑色,很闪眼睛,能不能优化一下做成加载过程全程都是黑色的插件?代码 ...

  3. spine网格拖动_可拖动的图像框网格

    spine网格拖动 View demo 查看演示Download Source 下载源 Today we want to create a template with a fullscreen gri ...

  4. 带网格的_装修要不要用网格布,了解这几点之后再做决定

    经常有朋友家里装修的时候,问我要不要贴网格布.网格布很多人只是听说过,对它的作用却知之甚少,今天乐爷就来和大家讲讲"网格布"的那些知识点.网格布是以中碱或无碱玻璃纤维纱为原料,织成 ...

  5. css 边缘闪光_纯css3闪烁动画《发光的边框效果》

    纯css3闪烁动画 /* 动画闪烁颜色 */ @-webkit-keyframes greenPulse { from { background-color: #749a02; -webkit-box ...

  6. vue在html中写style,vue开发之style(六)(CSS页面布局之样式、背景、文字)

    最近在学vue开发,并且将学习笔记与大家一起分享,前面讲了vue环境的搭建: 基于vscode的Vue前端环境搭建问题及解决办法 还有vue的使用,重点就在webpack这个: vue开发之webpa ...

  7. css grid布局_如何使用CSS Grid重新创建Medium的文章布局

    css grid布局 When people think of CSS Grid they normally envision image grid layouts and full web page ...

  8. [19/06/07-星期五] CSS基础_布局定位背景样式

    一.固定布局(不适应设备的浏览器的变化) <!DOCTYPE html> <html><head><meta charset="UTF-8" ...

  9. HTML怎么做成多彩背景,纯html+div+css模拟酷炫彩色渲染背景

    纯html+div+css模拟酷炫彩色渲染背景 www.html.com.cn 出品 html> 纯div+css模拟酷炫彩色渲染背景 /* 自写js */ body{ /*padding:0; ...

  10. CSS样式笔记_背景文本字体链接

    CSS背景属性: background:简写属性在一个声明中设置所有的背景属性.可以用于设置最底层的背景图片. background-attachment:属性设置背景图像是否固定或者随着页面的其余部 ...

最新文章

  1. 在不是Thread类的子类中,如何获取线程对象的名称呢?
  2. Java中关键字及其简要含义
  3. java做一个简单的数据库,哪个嵌入式数据库用Java写成一个简单的键/值存储?
  4. arm linux 交叉编译boost库
  5. 开发里程碑计划_如何通过里程碑控制项目进度
  6. LeetCode Convert Sorted List to Binary Search Tree 解题报告
  7. MySQL服务器安装完之后如何调节性能
  8. 常用nginx rewrite重定向-跳转实例
  9. 消息队列(MQ):ZeroMQ基本原理
  10. jquery事件绑定和解除绑定bind、unbind、one
  11. leetcode之四数相加
  12. 电工知识:常用电子元件名称及其对应图片实用大全
  13. 【FINAL】NOI
  14. 电路板常用连接器(接插件)介绍与选型建议(板对板连接器,板对线连接器,线对线连接器等)
  15. 自我介绍html模板王,小学生个人自我介绍模板10篇
  16. 体外诊断丨艾美捷游离维多珠单抗ADA水平检测试剂盒
  17. shell脚本运行报错: syntax error: unmatched ‘while‘
  18. Azure语音合成再添新声音,“风格迁移”技术为不同音色实现多情感演绎
  19. BIT-MiniCC——parser(lab5语法分析器)
  20. ubuntu安装和卸载软件命令

热门文章

  1. python求逆矩阵的方法,Python 如何求矩阵的逆
  2. android 限制后台进程,Android O Preview 相关-后台执行限制
  3. 路由器的级联(LAN-WAN)
  4. 艾永亮:酒瓶中的战争,谁是下一瓶被拿起的葡萄酒
  5. 3.计蒜客ACM题库.A1597 结果填空:年龄
  6. 嵌入式1553B总线开发板的应用
  7. java系列 - entity,vo转换
  8. 公链Sui Layer1网络
  9. Cisco2960交换机端口安全Port-Security
  10. ycf 梗_抖音三个汤勺放在筷子上是什么梗 抖音三个汤勺放在筷子上出处介绍[多图]...