gpt4 book ai didi

javascript - Cloudinary 响应式图像仅在使用 Webpack 的 React 中的窗口大小变化时显示

转载 作者:行者123 更新时间:2023-12-03 03:28:46 25 4
gpt4 key购买 nike

我按照本文档响应地提供 cloudinary 图像:

http://cloudinary.com/blog/how_to_automatically_create_images_for_responsive_design#implementing_responsive_design_with_cloudinary_s_javascript_library

我的index.hjs看起来像这样:

<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cloudinary-
core/2.3.0/cloudinary-core-shrinkwrap.js" type="text/javascript">
</script>
</head>
<body>
<div id="root"></div>
<script type="text/javascript" src="/static/bundle.js"></script>
<script type="text/javascript">
var cl = cloudinary.Cloudinary.new({cloud_name: "myCloudName"});
cl.responsive();
</script>
</body>

根据上述文档,组件渲染图像具有以下内容:

{this.state.images.map(function(image, i){
return <img key={i} className="cld-responsive thumbnail"
data-src={image.albums.images}/>})}

问题出在初始渲染上,图像渲染为 100% 宽度和 0 高度,并且根本不显示。但是,如果我调整窗口大小,则会显示响应图像。

我需要移动

 cl.responsive();

到我代码中的其他地方?我尝试将其移动到组件中并使用 componentDidMount() 触发状态刷新,但图像在重新渲染时仍然不显示;仅当窗口大小调整时。

感谢任何帮助。

最佳答案

当图像设置了 responsive 属性时,SDK 会在调整大小时自动更新图像。

示例代码:

<div className="image-holder">
<Image publicId="sample" responsive>
<Transformation width="auto" crop="scale"/>
</Image>
</div>

完整示例链接:https://codepen.io/eitanp461/project/editor/ZoNNkP

请注意,Cloudinary 的 React SDK 依赖于 cloudinary-core,因此您无需将其作为单独的脚本从 index.html 中加载。

关于javascript - Cloudinary 响应式图像仅在使用 Webpack 的 React 中的窗口大小变化时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46187326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com