根据提供的素材,这个网站是一个Gatsby.js构建的网站。它使用了Gatsby Image Wrapper组件来处理图像的布局和显示。 首先,我们来看一下Gatsby Image Wrapper组件的基本结构: ```html
``` 在这个结构中,``标签用于承载图片内容,而`
`标签则作为容器,将图片包裹起来。通过设置`overflow: hidden`属性,可以隐藏图片超出容器部分的内容,实现图片缩放效果。 接下来,我们来看一下CSS样式的应用: 1. `.gatsby-image-wrapper{position:relative;overflow:hidden}`:设置了`.gatsby-image-wrapper`的相对定位,并设置了溢出隐藏属性。 . `.gatsby-image-wrapper picture.object-fit-polyfill{position:static!important}`:设置了`.gatsby-image-wrapper`中的`picture`元素的固定位置(`static`),并强调了其重要性。 3. `.gatsby-image-wrapper img{bottom:0;height:100%;left:0;margin:0;max-width:none;padding:0;position:absolute;right:0;top:0;width:100%;object-fit:cover}`:设置了`.gatsby-image-wrapper`中的`img`元素的位置、尺寸、外边距等属性,使其符合图片的布局要求。同时,通过设置`object-fit: cover`属性,实现了图片自动填充容器的效果。 4. `.gatsby-image-wrapper [data-main-image]{opacity:0;transform:translateZ(0);transition:opacity .25s linear;will-change:opacity}`:在`.gatsby-image-wrapper`中添加了一个带有`data-main-image`属性的元素,用于指定主图的URL。通过设置`opacity: 0; transform: translateZ(0)`属性,使主图在页面加载时不可见;通过设置`transition: opacity .25s linear; will-change: opacity`属性,实现了主图透明度变化的过渡效果。 5. `.gatsby-image-wrapper-constrained{display:inline-block;vertical-align:top}`:为`.gatsby-image-wrapper-constrained`元素设置了内联块布局和垂直对齐属性,以适应不同浏览器的兼容性需求。 最后,我们可以使用以下JavaScript代码来实现图片的加载和展示: ```javascript import React from 'react'; import { gatsbyImage } from 'gatsby-image'; function MyComponent() { const [mainImage, setMainImage] = React.useState(''); React.useEffect(() => { const imageUrl = 'your-main-image-url'; // 替换为实际的主图URL setMainImage(gatsbyImage(imageUrl)); }, []); return (
Your Main Image
); } ``` 这样,我们就完成了一个简单的Gatsby.js网站示例,其中包含了图片的加载和展示功能。你可以根据需要对这个示例进行修改和扩展。