简体   繁体   English

Hover 通过保证金

[英]Hover through Margin

The HTML is as follows (I'm using React BTW): HTML 如下(我使用的是 React BTW):

 .iss { width: 100%; z-index: -1; position: absolute; left: 0; transition: all 0.5s ease-in-out; }.heading-section { margin-top: 250px; pointer-events: none; }.dgs-heading { color: white; font-family: Verdana, Geneva, Tahoma, sans-serif; margin: auto; }.dgs-para { color: white; font-family: Verdana, Geneva, Tahoma, sans-serif; }.iss:hover { -webkit-filter: blur(5px); filter: blur(5px); }
 <div class="dragon-sim"> <img class="iss" src="https://spacecraftearth.com/wp-content/uploads/2017/06/ISS-Earth-Mediterranean-2.jpg" alt="iss-img" /> <div class="heading-section"> <h1 class="dgs-heading">SpaceX Dragon 2 Simulator</h1> <p class="dgs-para">Dock the SpaceX Dragon 2 Satellite to the International Space Station in this realtime simulator with the controls our astronauts use on their missions.</p> <Button variant='outlined' class="dgs-btn"><a href="https://iss-sim.spacex.com/">Try Now</a></Button> </div> </div>

What I want is the heading section to be centered over the image.我想要的是标题部分以图像为中心。 The image is at z - index = -1.图像位于 z - index = -1。

The next thing is that the image to be blurred when hovered over.接下来是图像在悬停时模糊。 But the problem is that the padding or margin is getting in the way and is blocking the hover effect.但问题是填充或边距阻碍了 hover 效应。

Possibly this is what you're lopoking for.可能这就是你想要的。 Remove pointer-events: none;删除pointer-events: none; and change the position: absolute;并更改position: absolute; to the .heading-section instead of the image..heading-section而不是图像。 As well as add this code:以及添加此代码:

.dragon-sim {
  position: relative;
  text-align: center;
}

Here's a working example (I changed className to class just for this example to run, just change it back for react)这是一个工作示例(我将className更改为class只是为了运行此示例,只需将其更改回以进行反应)

Edit: Add this to your CSS to get a blur even when hovering over the header.编辑:将此添加到您的 CSS 中,即使将鼠标悬停在 header 上也会变得模糊。

.dragon-sim:hover .iss {
    -webkit-filter: blur(5px);
    filter: blur(5px);
}

Example here:这里的例子:

 .iss { width: 100%; z-index: -1; transition: all 0.5s ease-in-out; }.dragon-sim { position: relative; text-align: center; }.heading-section { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }.dgs-heading { color: white; font-family: Verdana, Geneva, Tahoma, sans-serif; margin: auto; }.dgs-para { color: white; font-family: Verdana, Geneva, Tahoma, sans-serif; }.dragon-sim:hover.iss { -webkit-filter: blur(5px); filter: blur(5px); }
 <div class="dragon-sim"> <img class="iss" src="https://spacecraftearth.com/wp-content/uploads/2017/06/ISS-Earth-Mediterranean-2.jpg" alt="iss-img"/> <div class="heading-section"> <h1 class="dgs-heading">SpaceX Dragon 2 Simulator</h1> <p class="dgs-para">Dock the SpaceX Dragon 2 Satellite to the International Space Station in this realtime simulator with the controls our astronauts use on their missions.</p> <Button variant='outlined' class="dgs-btn"><a href="https://iss-sim.spacex.com/">Try Now</a></Button> </div> </div>

First, please indent your html code when asking for question.首先,请在提问时缩进您的 html 代码。

try the following to center .heading-section尝试以下以居中.heading-section

.heading-section {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

If you change the .heading-section to position: absolute , and pointer-events: none , the hover will pass through to the underlying image.如果将.heading-section更改为position: absolutepointer-events: none ,则 hover 将传递到底层图像。

View this in full page mode:以全页模式查看:

 .iss { width: 100%; z-index: -1; position: absolute; left: 0; top: 50px; transition: all 0.5s ease-in-out; } /* heading overlay: use absolute positioning */.heading-section { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); pointer-events: none; /* passes hover to img */ }.dgs-heading { color: white; font-family: Verdana, Geneva, Tahoma, sans-serif; margin: auto; }.dgs-para { color: white; font-family: Verdana, Geneva, Tahoma, sans-serif; } /* override "pointer-events: none" to allow button click */.dgs-btn { pointer-events: auto; }.iss:hover { -webkit-filter: blur(5px); filter: blur(5px); }
 <div class="dragon-sim"> <img class="iss" src="https://spacecraftearth.com/wp-content/uploads/2017/06/ISS-Earth-Mediterranean-2.jpg" alt="iss-img"/> <div class="heading-section"> <h1 class="dgs-heading">SpaceX Dragon 2 Simulator</h1> <p class="dgs-para">Dock the SpaceX Dragon 2 Satellite to the International Space Station in this realtime simulator with the controls our astronauts use on their missions.</p> <button class="dgs-btn" onclick="alert('click')">Try Now</button> </div> </div>

You need to have a wrapper which contains both the image and the header and, when that wrapper is hovered on you then blur the image.您需要一个包含图像和 header 的包装器,并且当该包装器悬停在您身上时,然后模糊图像。

Naturally you can centre the heading-section in the way Kevin mentioned.当然,您可以按照 Kevin 提到的方式将标题部分居中。

So based on what I said my solution would be, same html and the css would become something like:所以根据我所说的,我的解决方案是,同样的 html 和 css 会变成这样:

.iss {
    width: 100%;
    z-index: -1;
    left: 0;
    transition: all 0.5s ease-in-out;
}

.heading-section {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.dgs-heading {
    color: white;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
    margin: auto;
}

.dgs-para {
    color: white;
    font-family: Verdana, Geneva, Tahoma, sans-serif;
}

.iss:hover {
    -webkit-filter: blur(5px);
    filter: blur(5px);
}

.dragon-sim {
    position: relative;
}

.dragon-sim:hover > .iss {
    -webkit-filter: blur(5px);
    filter: blur(5px);
}

This seems to be doing what you wanted, it blurs the image but not the text and you can still interact with the text and button no problem.这似乎在做你想做的事,它模糊了图像而不是文本,你仍然可以与文本和按钮进行交互,没问题。

Also the text is not very readable I would suggest putting some transparent background for the heading-section此外,文本不是很易读,我建议为标题部分放置一些透明背景

Also note that I removed the pointer-events: none;另请注意,我删除了pointer-events: none; as that is no longer necessary, plus with that you would not be able to click the button, so you definitely do not want to use that property因为这不再是必需的,而且您将无法单击该按钮,因此您绝对不想使用该属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM