使用 object-fit 时如何删除 <img> 的背景层:contain;

如何解决使用 object-fit 时如何删除 <img> 的背景层:contain;

我正在尝试制作图像查看器,它具有中心和 div 的叠加窗口,一个图像在窗口调整大小时上下缩放,通过使用 object-fit:contain; 保持其默认比例;这是工作正常。问题是当使用 object-fit:contain;它缩放内容,并且具有类似背景层的内容,阻止我单击覆盖窗口,因此我可以关闭视图。是否可以裁剪、剪切自动调整大小,使其始终与内容一样大。也许使用clip:rect();。提前致谢。

我正在尝试删除蓝色背景,以便我可以单击背景并关闭叠加窗口,但仍然可以在不关闭叠加层的情况下单击图像。

enter image description here

示例https://jsfiddle.net/qwdnkxLt/

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Show Image</title>

    <style>
        html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            /* --scrollbarBG: rgb(70,103,109);
            --thumbBG: rgba(81,74,85,0.78); */
        }




        /*--------BODY--------------------------------------------------------------------*/
        body {
            margin: 0;
            padding: 0;
            background-color: rgb(40,37,44);
            width: 100%;
            height: 100%;
        }


        /* --------Content - Item---IMG-----*/
        .contentContainer_Item {
            /* width: 250px;
            height: 150px; */

            width: 17em;
            height: 10em;
            box-shadow: 0 0 10pt 3pt rgb(0,0);
            /* filter: drop-shadow(0 0.2rem 0.25rem rgb(0,0)); */
            border-radius: 5px;
            border: solid 2px rgb(17,17,22);
            transition: transform 0.5s ease;
            display: block;
        }






        .contentContainer_Item:hover {
            box-shadow: 0 0 5pt 2pt rgba(33,182,216,0.39);
            border: solid 2px rgb(29,221,189);
            border-radius: 5px;
            filter: saturate(3)
        }







        .contentContainer_Item_Margin {
            margin: 20px;
            float: left;
            text-align: center;
            color: rgb(5,163,255);
            text-decoration: none;
            font-family: 'Segoe UI',Tahoma,Geneva,Verdana,sans-serif;
            font-weight: bold;
        }



        .contentContainer_Item_Margin:hover {
            color: rgb(5,255,255);
        }







        .contentContainer_Item_Holder {
            position: relative;
            display: inline-block;
        }



        .contentContainer_Item_Holder_Image_Viewer {
            width: 50px;
            height: 50px;
            position: absolute;
            z-index: 4;
            bottom: 50px;
            right: 30px;
            cursor: zoom-in;
            border-radius: 8px;
            box-shadow: 0 0 5px 5px rgba(0,217,0.811);
            border: 1px solid rgb(112,197,236);

        }

        img.view-Img-Button {
            content: url('https://iso.500px.com/wp-content/uploads/2016/03/stock-photo-142984111.jpg');
        }
    </style>
</head>

<body>

    <!--Overlay-Window------------------------------->
    <div id="overlayWindow" onmousedown="this.style.display='none';"
        style="display: none; background-color: rgba(9,21,34,0.7); position: fixed; z-index: 100;  background: url(https://thumbs.dreamstime.com/b/environment-earth-day-hands-trees-growing-seedlings-bokeh-green-background-female-hand-holding-tree-nature-field-gra-130247647.jpg) no-repeat center center fixed;  background-size: 100% 100%;  width: 100%; height: 100%;  top: 0; left: 0;  bottom: 0; right: 0;">


        <!--Overlay--WIndow-Content--Container------------------------------>
        <div  style="position: relative; top: 50%; left: 50%;  transform: translate(-50%,-50%); background-color:rgb(255,168,68); width: 80%;  height: 80%;"> 
            <img onmousedown="event.stopPropagation();" id="overlayImg"
                style="position: absolute; border-radius: 100px;  background-color:rgba(24,129,190,0.938); object-fit: contain;  width: 100%; height: 100%;" />
        </div>

    </div>




    <!--Holder::::::::::::::::::::::::::::::::::::::::::::::-------------------------------->
    <div class="contentContainer_Item_Holder">
        <!--Img Viewer Button-------------------------------->
        <img onmousedown="ViewImage('calcArea')"
            class="view-Img-Button contentContainer_Item_Holder_Image_Viewer"></img>

        <!--Img---------------------------------------------->
        <a href="https://github.com/stefan27dk/AutoFOCUS" target="_blank" class="contentContainer_Item_Margin">
            <img id="calcAreaThumb" alt="Calc. Area of Graf" class="contentContainer_Item"></img>
            Calc. Area of graf - "JS"</a>
    </div>






    <!---Images--Storage------------------------------------------------>
    <script>
        var calcArea = 'https://helpx.adobe.com/content/dam/help/en/lightroom-cc/how-to/share-photos-on-web-gallery/_jcr_content/main-pars/image/share-photos-on-web-gallery_1800x1012.jpg';
        document.getElementById('calcAreaThumb').style.content = `url(${calcArea})`;
    </script>




    <!---Overlay Window---::JS::--------------------------------------------->
    <script>
        function ViewImage(imgName) {
            document.getElementById("overlayWindow").style.display = 'block'; // Show overlayWindow
            document.getElementById('overlayImg').src = window[imgName]; // Static img Tag
        }
    </script>

</body>

</html>

解决方法

解决方案 1

将任何对象的位置设置为 absolute 有一点危险:这使它几乎忽略了它与其他对象在位置、比例等方面的任何关系。

因此,我建议您为图像容器使用 position: relative; 并允许两个维度之一(我建议使用 height: 100%width: inheritauto)进行缩放自动保持纵横比。现代浏览器足够聪明,通常可以正确填充给定的区域而不会留下任何空白。
另外,请记住使用 margin: auto; 将您的对象很好地居中。

这应该可以解决您的问题:

<!--Overlay--WIndow-Content--Container------------------------------>
<div  style="display: block; position: relative; top: 50%; left: 50%;  transform: translate(-50%,-50%); background-color:rgb(255,168,68); width: auto;  height: 80%;"> 
    <img onmousedown="event.stopPropagation();" id="overlayImg"
        style="
        display: block;
        position: relative;
        margin: auto;
        border-radius: 100px;
        background-color:rgba(24,129,190,0.938);
        object-fit: contain;
        width: inherit;
        height: 100%;
        " />
        </div>
    </div>

JSFiddle 在这里:https://jsfiddle.net/uewog42m/25/

最后一个想法:如果将 CSS 和 HTML/JS 分开,调试会更容易!考虑为所有 CSS 代码使用单独的样式表。

解决方案 2

解决此问题的另一种方法是为图像使用容器并将其用于缩放。然后,将图像放置在容器内并使用父级的尺寸。最后,切断溢出(如果有)。 或者,如果您不想剪切图像的一部分,请不要使用宽度/高度的最大值并将图像居中。

它应该看起来像这样:

<!--Overlay--WIndow-Content--Container------------------------------>
<div  style="display: block; position: relative; top: 50%; left: 50%;  transform: translate(-50%,68); width: auto;  height: 80%;">
    <div id="wrapper" style="
        display: block;
        position: relative;
        border-radius: 100px;
        overflow: hidden;
        margin: 0;
    ">
        <img onmousedown="event.stopPropagation();" id="overlayImg"
            style="
            display: block;
            position: relative;
            background-color:rgba(24,0.938);
            object-fit: contain;
            width: 100%;
            height: 100%;
        " />
    </div>
</div>
,

对我有用的解决方案: enter image description here

e

片段:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Show Image</title>

    <style>
        html {
            margin: 0;
            padding: 0;
            width: 100%;
            height: 100%;
            /* --scrollbarBG: rgb(70,103,109);
            --thumbBG: rgba(81,74,85,0.78); */
        }




        /*--------BODY--------------------------------------------------------------------*/
        body {
            margin: 0;
            padding: 0;
            background-color: rgb(40,37,44);
            width: 100%;
            height: 100%;
        }


        /* --------Content - Item---IMG-----*/
        .contentContainer_Item {
            /* width: 250px;
            height: 150px; */

            width: 17em;
            height: 10em;
            box-shadow: 0 0 10pt 3pt rgb(0,0);
            /* filter: drop-shadow(0 0.2rem 0.25rem rgb(0,0)); */
            border-radius: 5px;
            border: solid 2px rgb(17,17,22);
            transition: transform 0.5s ease;
            display: block;
        }






        .contentContainer_Item:hover {
            box-shadow: 0 0 5pt 2pt rgba(33,182,216,0.39);
            border: solid 2px rgb(29,221,189);
            border-radius: 5px;
            filter: saturate(3)
        }







        .contentContainer_Item_Margin {
            margin: 20px;
            float: left;
            text-align: center;
            color: rgb(5,163,255);
            text-decoration: none;
            font-family: 'Segoe UI',Tahoma,Geneva,Verdana,sans-serif;
            font-weight: bold;
        }



        .contentContainer_Item_Margin:hover {
            color: rgb(5,255,255);
        }







        .contentContainer_Item_Holder {
            position: relative;
            display: inline-block;
        }



        .contentContainer_Item_Holder_Image_Viewer {
            width: 50px;
            height: 50px;
            position: absolute;
            z-index: 4;
            bottom: 50px;
            right: 30px;
            cursor: zoom-in;
            border-radius: 8px;
            box-shadow: 0 0 5px 5px rgba(0,217,0.811);
            border: 1px solid rgb(112,197,236);

        }

        img.view-Img-Button {
            content: url('https://iso.500px.com/wp-content/uploads/2016/03/stock-photo-142984111.jpg');
        }
    </style>
</head>

<body>

    <!--Overlay-Window------------------------------->
    <div id="overlayWindow" onmousedown="this.style.display='none';"
        style="
        display: none; 
        background-color: 
        rgba(9,21,34,0.7);
         position: fixed; 
         z-index: 100;  
         background: url(https://thumbs.dreamstime.com/b/environment-earth-day-hands-trees-growing-seedlings-bokeh-green-background-female-hand-holding-tree-nature-field-gra-130247647.jpg) 
         no-repeat center center fixed;  
         background-size: 100% 100%;  
         width: 100%; 
         height: 100%;  
         top: 0; 
         left: 0;  
         right: 0;
         bottom: 0;">


        <!--Overlay--WIndow-Content--Container------------------------------>
        <div  style="display: block; 
        position: relative; 
        top: 50%; 
        left: 50%;  
        transform: translate(-50%,-50%); 
        background-color:rgb(255,68);
        max-width: 80vw;
        max-height: 80vh"> 


            <img onmousedown="event.stopPropagation();" id="overlayImg"
        style="
        display: block;
        position: relative;
        margin: auto;
        border-radius: 100px;
        background-color:rgba(24,0.938);
        object-fit: contain;
        width: auto;
        height: auto;
        max-height: 80vh;
        max-width: 80vw;
        " />
        </div>

    </div>




    <!--Holder::::::::::::::::::::::::::::::::::::::::::::::-------------------------------->
    <div class="contentContainer_Item_Holder">
        <!--Img Viewer Button-------------------------------->
        <img onmousedown="ViewImage('calcArea')"
            class="view-Img-Button contentContainer_Item_Holder_Image_Viewer"></img>

        <!--Img---------------------------------------------->
        <a href="https://github.com/stefan27dk/AutoFOCUS" target="_blank" class="contentContainer_Item_Margin">
            <img id="calcAreaThumb" alt="Calc. Area of Graf" class="contentContainer_Item"></img>
            Calc. Area of graf - "JS"</a>
    </div>






    <!---Images--Storage------------------------------------------------>
    <script>
        var calcArea = 'https://helpx.adobe.com/content/dam/help/en/lightroom-cc/how-to/share-photos-on-web-gallery/_jcr_content/main-pars/image/share-photos-on-web-gallery_1800x1012.jpg';
        document.getElementById('calcAreaThumb').style.content = `url(${calcArea})`;
    </script>




    <!---Overlay Window---::JS::--------------------------------------------->
    <script>
        function ViewImage(imgName) {
            document.getElementById("overlayWindow").style.display = 'block'; // Show overlayWindow
            document.getElementById('overlayImg').src = window[imgName]; // Static img Tag
        }
    </script>

</body>

</html>

没有额外的 Div 更干净:

<!--Overlay--WIndow-Content--Container------------------------------>
        <div  style="display: block; 
        position: relative; 
        top: 50%; 
        left: 50%;  
        transform: translate(-50%,0.938);
        object-fit: contain;
        width: auto;
        height: auto;
        max-height: 80vh;
        max-width: 80vw;
        " />
        </div>

所以实际上我不需要对象适合:包含;但只有

    <img onmousedown="event.stopPropagation();" id="overlayImg" style="
    position: relative;
    border-radius: 10px;
    border: 2px solid rgb(83,82,99);
    top: 50%; 
    left: 50%;  
    transform: translate(-50%,-50%); 
    max-height: 80vh;
    max-width: 80vw;
    " />

以及要调整大小的小图像

    max-height: 80vh; 
    max-width: 80vw;

不敢相信就这么简单。它在窗口调整大小时调整大小并保持其比率,已使用 16:9、1:1 和 9:16 进行测试。

完全有效的代码:---------------------------------------- ------------------

enter image description here

    min-height:auto;
    min-width:  50vh;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-