首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CSS: Alternative min()

CSS: Alternative min()
EN

Stack Overflow用户
提问于 2020-05-06 14:00:59
回答 1查看 450关注 0票数 1

根据div的大小,我想设置background-position-xbackground-size。仅使用没有JS或JQuery的CSS,我使用分钟(),它可以:

代码语言:javascript
运行
复制
.test {
  transition: all 0.5s;
  background-image: url(./my-asset.svg);
  background-repeat: no-repeat;
  background-position-x: min(-50px, -100%);
  background-position-y: center;
  background-size: min(50px, 100%) 100%;
}

这是正常工作的,但只适用于最近的浏览器,但我有一个Firefox目标为v.68,而且它不兼容。如果不使用JS或JQuery,而只使用CSS,还有什么可供选择的呢?

我使用min()在输出中复制我想要的内容。将红色部分悬停以使其工作:

代码语言:javascript
运行
复制
body,
html {
  height: 100%;
  width: 100%;
  margin: 5px;
}

#main {
  display: flex;
  flex-direction: row;
  background-color: cyan;
}

.use-px {
  width: 300px;
  height: 300px;
  border: solid 3px black;
}

.use-percentage {
  margin-left: 200px;
  width: 100px;
  height: 300px;
  border: solid 3px black;
}

.left-over-image {
  width: 25%;
  height: 100%;
  transition: all 1s;
  background-color: red;
  background-image: url(https://www.w3schools.com/images/w3schools_green.jpg);
  background-repeat: no-repeat;
  background-position-x: min(-50px, -100%);
  background-position-y: center;
  background-size: min(50px, 100%) 100%;
}

.left-over-image:hover {
  background-position: left;
}
代码语言:javascript
运行
复制
<html>

<body>
  <div id="main">
    <div class="use-px">
      <!-- It will use 50px, because 25% of 300px is 75px. -->
      <div class="left-over-image"></div>
    </div>
    <div class="use-percentage">
      <!-- It will use 100%, because 25% of 100px is 25px. -->
      <div class="left-over-image"></div>
    </div>
  </div>
</body>

</html>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-06 14:08:33

您可以考虑使用伪元素的技巧。

调整两个示例的大小以确保它们的行为相同:

代码语言:javascript
运行
复制
.box {
  height:100px;
  width:100px;
  border:2px solid;
  resize:both;
  overflow:hidden;
  background:linear-gradient(red,blue) 0/50px 50px no-repeat;
  background-position-x: min(4em, 100%);
}

.alt {
  height:100px;
  width:100px;
  border:2px solid;
  resize:both;
  overflow:hidden;
  position:relative;
  z-index:0;
}
.alt::before {
  content:"";
  position:absolute;
  background:inherit;
  background:linear-gradient(red,blue) 0/50px 50px no-repeat;
  background-position-x:100%;
  max-width:calc(4em + 50px); /* 4em + width of background */
  width:100%;
  top:0;
  bottom:0;
  left:0;
  z-index:-1;
}
代码语言:javascript
运行
复制
<div class="box">

</div>

<div class="alt">

</div>

更新

根据您的新代码:

代码语言:javascript
运行
复制
body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
  overflow: hidden;
}

#main {
  display: flex;
  flex-direction: row;
  background-color: cyan;
}

.use-px {
  width: 300px;
  height: 300px;
  border: solid 1px black;
}

.use-percentage {
  width: 100px;
  height: 300px;
  border: solid 1px black;
}

.left-over-image {
  width: 25%;
  height: 100%;
  background-color: red;
  position:relative;
  overflow:hidden;
}
.left-over-image::before {
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  width:100%;
  max-width:50px;
  background-image: url(https://www.w3schools.com/images/w3schools_green.jpg);
  background-size: 100% 100%;
  transform:translateX(-100%);
  transition: all 1s;
}
.left-over-image:hover::before {
  transform:translateX(0);
}
代码语言:javascript
运行
复制
<div id="main">
  <div class="use-px">
    <!-- It will use 50px, because 25% of 300px is 75px. -->
    <div class="left-over-image"></div>
  </div>
  <div class="use-percentage">
    <!-- It will use 100%, because 25% of 100px is 25px. -->
    <div class="left-over-image"></div>
  </div>
</div>

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61637143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档