html - 在没有 srcset 的情况下反应响应式图像?
问题描述
我想确认 React 是否能够在不需要 html 中的 srcset 标签的情况下提供响应式图像。
使用带有窗口宽度的状态,我可以设置图像的条件渲染。使用 chrome 并检查网络流量,似乎正在提供所需的图像,而无需提供所有图像。
import React from "react"
import images from "../assets/images"
class App extends React.Component{
state = {
width: 0
}
componentDidMount(){
window.addEventListener("resize", this.handleResize)
this.setState({
width: window.innerWidth
})
console.log(this.state.width)
}
handleResize = () =>{
this.setState({
width: window.innerWidth
})
console.log(this.state.width)
}
render(){
const image = this.state.width > 1000 && this.state.width !== 0 ? images.HD : images.LD
return(
<div>
<img src={image} alt="hd/ld"/>
</div>
)
}
}
export default App
这是提供具有不同分辨率的特定图像的有效方法吗?
解决方案
推荐阅读
- jquery - 在 kendo ui 中创建或编辑后返回警报
- matlab - 如何在图形 Matlab App Designer 上获得下拉菜单
- c# - 水晶报表设置参数时出现“系统找不到指定的路径”错误
- mariadb - SQL order by “id”导致重复,而另一列排序工作正常
- python - 如何一起添加递增的整数/字符串行?
- c++ - c++ const_cast gcc 8.2.1 -Wignored-qualifiers bug?
- tfs - 如何批量放弃 TFS 代码审查?
- google-apps-script - 在 Google 表格中将两个单元格值链接在一起
- java - 使用 MongoDB 驱动程序保存时出现异常
- laravel-5.5 - getStream laravel 集成 FeedConfigException