为了账号安全,请及时绑定邮箱和手机立即绑定

如何停止悬停过渡中元素的重叠?

如何停止悬停过渡中元素的重叠?

吃鸡游戏 2021-12-23 16:55:52
在我的网站中,我希望能够允许用户将鼠标悬停在图像上并通过过渡放大图像。我已经能够成功实现过渡,但是,当图像被放大时,它不断地与其他元素重叠。我当前的布局是在网格中排序的,并且容器已被赋予属性溢出:隐藏。我试图在每个元素悬停时为其分配 z-index 值 -1,但是层之间存在连续变化,这看起来很可怕。如何允许放大每个图像而不与任何其他元素重叠?这是我的 jsfiddle:https ://jsfiddle.net/Syed213shah/4u0vh5Lb/body {  background-color: #800020;}body, html {  height: 100%;  margin: 0;}  #box-container {    display: flex;    height: 600px;    width: 75%;  }.container {  min-height: 500px;  width: 100%;  display: grid;  grid-template-columns: 50% 2fr;  grid-template-rows: 50% 2fr;  overflow: hidden;  position: static;}.item1 {   background-image: url('https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg');  width: 100%;  height: 200%;  transition: all 0.5s ease-in-out;   position: relative;}.item1:hover {   transform: scale(1.1);    z-index: -1;}.item2 {   background-image: url('https://media-public.fcbarcelona.com/20157/0/document_thumbnail/20197/172/175/246/32944044/1.0-10/32944044.jpg?t=1475841685000');  grid-column: 2;  grid-row: 2;  width: 100%;  height: 400px;  transition: all 0.5s ease-in-out;   position: relative;}.item2:hover {   transform: scale(1.1);    z-index: -1;}.item3 {  background-image: url('https://e00-marca.uecdn.es/assets/multimedia/imagenes/2019/10/26/15721062687641.jpg');  grid-column: 2;  grid-row: 1;  width: 100%;  height: 400px;  transition: all 0.5s ease-in-out;   position: relative;}.item3:hover {   transform: scale(1.1);   z-index: -1;}
查看完整描述

2 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

我认为使用伪元素或内部标签(如您所愿)并缩放此元素设置其父元素(我们的<a>)更简单,overflow:hidden;以防止您的错误。


在我的例子中,我使用了一个伪元素。我将这些代码行添加到您的 CSS(我还注释了一些行):


.container a {

  overflow: hidden;

}


.container a::after {

  height:100%;

  width:100%;

  content: "";

  position: absolute;

  transition: all 0.5s ease-in-out; 

  z-index:-1;

}


.item1::after{

  background-image: url('https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg');

}

body {

  background-color: #800020;

}


body, html {

  height: 100%;

  margin: 0;

}


#box-container {

  display: flex;

  height: 600px;

  width: 75%;

}



/* https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg  */


/* https://media-public.fcbarcelona.com/20157/0/document_thumbnail/20197/172/175/246/32944044/1.0-10/32944044.jpg?t=1475841685000 */


/* https://e00-marca.uecdn.es/assets/multimedia/imagenes/2019/10/26/15721062687641.jpg */



.container {

  min-height: 500px;

  width: 100%;

  display: grid;

  grid-template-columns: 50% 2fr;

  grid-template-rows: 50% 2fr;

  overflow: hidden;

  position: static;

}


.item1 { 

  /*background-image: url('https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg');*/

  width: 100%;

  height: 200%;

  /*transition: all 0.5s ease-in-out;*/ 

  position: relative;

}


/*.item1:hover {

   transform: scale(1.1); 

   z-index: -1;

}*/


.item2 { 

  /*background-image: url('https://media-public.fcbarcelona.com/20157/0/document_thumbnail/20197/172/175/246/32944044/1.0-10/32944044.jpg?t=1475841685000');*/

  grid-column: 2;

  grid-row: 2;

  width: 100%;

  height: 400px;

  /*transition: all 0.5s ease-in-out; */

  position: relative;

}


/*.item2:hover {

   transform: scale(1.1); 

   z-index: -1;

}*/


.item3 {

  /*background-image: url('https://e00-marca.uecdn.es/assets/multimedia/imagenes/2019/10/26/15721062687641.jpg');*/

  grid-column: 2;

  grid-row: 1;

  width: 100%;

  height: 400px;

  /*transition: all 0.5s ease-in-out; */

  position: relative;

}


/* -------------------------- */

/* I added these lines of code */

/* -------------------------- */


.container a {

  overflow: hidden;

}


.container a::after {

  height:100%;

  width:100%;

  content: "";

  position: absolute;

  transition: all 0.5s ease-in-out; 

  z-index:-1;

}


.item1::after{

  background-image: url('https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg');

  

  /*to set a background without repetition and always horizontally center you could use also this syntaxt:

  background: url('https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg') 50% top no-repeat transparent;

  */

}


.item2::after{

  background-image: url('https://media-public.fcbarcelona.com/20157/0/document_thumbnail/20197/172/175/246/32944044/1.0-10/32944044.jpg?t=1475841685000');

}


.item3::after{

  background-image: url('https://e00-marca.uecdn.es/assets/multimedia/imagenes/2019/10/26/15721062687641.jpg');

}


.container a:hover::after{

  transform: scale(1.1);

}


/* -------------------------- */

/* I added these line of code */

/* -------------------------- */


.item1 h2 {

  font-size: 50px;

  position: absolute;

  font-family: Staatliches;

  text-align: center;

  color: white;

  text-decoration: none;

  padding: 500px 70px;

}


.item2 h2 {

  font-size: 50px;

  position: absolute;

  font-family: Staatliches;

  text-align: center;

  color: white;

  text-decoration: none;

  padding: 200px 200px;

}


.item3 h2 {

  font-size: 50px;

  position: absolute;

  font-family: Staatliches;

  text-align: center;

  color: white;

  text-decoration: none;

  padding: 185px 200px;

}


.full-height {

  height: 100%;

}

.bottom-height {

  height: 100%;

}


h1 {

  font-size: 50px;

  font-family: Staatliches;

  text-align: center;

  color: #002a58;

}


#navbar {

  background-color: #800020;

  position: fixed;

  top: -30px;

  width: 100%;

  transition: top 0.3s;

}


#navbar ul {

  height: -30px; 

  padding: 10px 0 10px 40px; 

  width: 100%; 

}


#navbar li{

  float: left;

  line-height: 20px;

  margin-right: 30px;

  padding: 10px 3px;

  position: relative;

  list-style-type: none;

}


#navbar li a {

  font-family: Staatliches;

  text-decoration: none;

  color: rgb(13, 11, 134);

}


#navbar li a:hover {

  background-color: #ddd;

  color: black;

}

<body>

   <div class="full-height">

      <script src="script.js"></script>

      <div class="container">

         <a class="item1" href="https://www.bbc.co.uk/sport/football" style="text-decoration: none;" >

            <h2> Europe's biggest stadium  </h2>

         </a>

         <a class="item2" href="https://www.fcbarcelona.com/en/" style="text-decoration: none;" >

            <h2>European Success</h2>

         </a>

         <a class="item3" href="https://www.fcbarcelona.com/en/football/first-team/news" style="text-decoration: none;" >

            <h2>Current Squad</h2>

         </a>

      </div>

      <div id="navbar">

         <ul>

            <li><a href="https://first-website.awais.repl.co/">Home</a></li>

            <li><a href="news.asp">Squad</a></li>

            <li><a href="contact.asp">Contact</a></li>

            <li><a href="about.asp">About</a></li>

            <a2><a>Created by Awais</a></a2>

         </ul>

      </div>

      <h1>FC Barcelona</h1>

   </div>

   <div class="bottom-height">

   </div>

</body>

.item2::after{

  background-image: url('https://media-public.fcbarcelona.com/20157/0/document_thumbnail/20197/172/175/246/32944044/1.0-10/32944044.jpg?t=1475841685000');

}


.item3::after{

  background-image: url('https://e00-marca.uecdn.es/assets/multimedia/imagenes/2019/10/26/15721062687641.jpg');

}


.container a:hover::after{

  transform: scale(1.1);

}

我没有碰你的 HTML。


查看完整回答
反对 回复 2021-12-23
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

而不是变换:在您的图像上缩放,也许使用背景大小和背景位置可能会给您寻求的结果,并对您已经使用的实际裁剪进行更多控制。


附带的 jsfiddle 使用这样的示例修改了您的代码。我确实为文本叠加保留了变换比例。还要注意图像容器需要一个 overflow:hidden 以防止单元格之间的悬停交互。


这是您相应修改的 css;


body {

  background-color: #800020;

}


body, html {

  height: 100%;

  margin: 0;

}


#box-container {

    display: flex;

    height: 600px;

    width: 75%;

  }


.container {

  min-height: 500px;

  width: 100%;

  display: grid;

  grid-template-columns: 50% 2fr;

  grid-template-rows: 50% 2fr;

  overflow: hidden;

  position: static;

}


.item1 {

  background-image: url('https://pbs.twimg.com/media/D5gQCxCW0AE0skl.jpg');

 background-position: 0% 50%;

 background-size:200%;

  width: 100%;

  height: 200%;

  transition: all 0.5s ease-in-out;

  position: relative;

  overflow: hidden;

}


.item1:hover {

   background-size:220%;

   background-position: 5% 50%;

}


.item2 {

  background-image: url('https://media-public.fcbarcelona.com/20157/0/document_thumbnail/20197/172/175/246/32944044/1.0-10/32944044.jpg?t=1475841685000');

  background-position: 0% 50%;

  background-size:165%;

  grid-column: 2;

  grid-row: 2;

  width: 100%;

  height: 400px;

  transition: all 0.5s ease-in-out;

  position: relative;

  overflow: hidden;

}


.item2:hover {

   background-position: 5% 50%;

   background-size:180%;

}


.item3 {

  background-image: url('https://e00-marca.uecdn.es/assets/multimedia/imagenes/2019/10/26/15721062687641.jpg');

  background-position: 0% 15%;

  background-size:175%;

  grid-column: 2;

  grid-row: 1;

  width: 100%;

  height: 400px;

  transition: all 0.5s ease-in-out;

  position: relative;

  overflow: hidden;

}


.item3:hover {

   background-position: 5% 15%;

   background-size:195%;

}


.item1 h2 {

  font-size: 50px;

  position: absolute;

  font-family: Staatliches;

  text-align: center;

  color: white;

  text-decoration: none;

  padding: 500px 70px;

  transform: scale(1);

  transition: all 0.5s ease-in-out;

}


.item2 h2 {

  font-size: 50px;

  position: absolute;

  font-family: Staatliches;

  text-align: center;

  color: white;

  text-decoration: none;

  padding: 200px 200px;

  transform: scale(1);

  transition: all 0.5s ease-in-out;

}


.item3 h2 {

  font-size: 50px;

  position: absolute;

  font-family: Staatliches;

  text-align: center;

  color: white;

  text-decoration: none;

  padding: 185px 200px;

  transform: scale(1);

  transition: all 0.5s ease-in-out;

}


.item1:hover h2,

.item2:hover h2,

.item3:hover h2 {

   transform: scale(1.1);

}


.full-height {

  height: 100%;

}


.bottom-height {

  height: 100%;

}


h1 {

  font-size: 50px;

  font-family: Staatliches;

  text-align: center;

  color: #002a58;

}


#navbar {

  background-color: #800020;

  position: fixed;

  top: -30px;

  width: 100%;

  transition: top 0.3s;

}


#navbar ul {

  height: -30px;

  padding: 10px 0 10px 40px;

  width: 100%;

}


#navbar li{

  float: left;

  line-height: 20px;

  margin-right: 30px;

  padding: 10px 3px;

  position: relative;

  list-style-type: none;

}


#navbar li a {

  font-family: Staatliches;

  text-decoration: none;

  color: rgb(13, 11, 134);

}


#navbar li a:hover {

  background-color: #ddd;

  color: black;

}


#navbar .a2{

  float: right;

  line-height: 20px;

  margin-right: 50px;

  padding: 10px 3px;

  position: relative;

  list-style-type: none;

}


#navbar .a2 a {

  font-family: Staatliches;

  text-decoration: none;

  color: rgb(13, 11, 134);

}

https://jsfiddle.net/w9n6ajq1/


查看完整回答
反对 回复 2021-12-23
  • 2 回答
  • 0 关注
  • 117 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信