简体   繁体   English

如何使基本形状的CSS悬停效果?

[英]How to make base shape CSS hover effect?

According to web design I should make the css hover effect. 根据网页设计,我应该使css hover效果。

Currently I'm done doing the CSS shape for hover effect, however I don't really know how to implement this into hover effect. 目前,我已经完成了用于悬停效果的CSS形状,但是我真的不知道如何将其实现为悬停效果。

Should I use JavaScript for that or might be there are other ways to achieve this? 我应该为此使用JavaScript还是有其他方法可以实现此目的? Please, help me this, I'm really stuck with this. 请帮帮我,我真的很坚持。 For better understanding, the final version should look like this in the picture below: 为了更好地理解,最终版本应如下图所示:

在此处输入图片说明

Here is an example of CSS shape, that I need to implement as a hover effect. 这是CSS形状的示例,我需要将其实现为悬停效果。

 #base { background: #0a863d; display: inline-block; height: 66px; margin-top: 20px; margin-right: 55px; position: relative; width: 500px; } #base:before { border-left: 35px solid #0a863d; border-top: 33px solid transparent; border-bottom: 33px solid transparent; content: ""; height: 0; top: 0; position: absolute; right: -35px; width: 0; } 
 <div id="base"></div> 

To this HTML code I need to implement hover effect => 对于此HTML代码,我需要实现悬停效果=>

<div class="left-main col-lg-3 col-md-3 col-sm-12 col-xs-12">
  <div class="shadow-effect"><p class="ottogi">OTTOGI</p></div>
  <div class="shadow-effect"><p class="sajo">Sajo Hapyo</p></div>
  <div class="shadow-effect"><p class="natura">Natura Bogata</p></div>
  <div class="shadow-effect"><p class="maloo">ТОО Малу</p></div>
  <div class="shadow-effect"><p class="dongush">Dongsuh</p></div>
  <div class="shadow-effect"><p class="may">ООО Май</p></div>
</div>

You just need to change background-color on hover and show triangle using pseudoelement. 您只需要更改悬停时的background-color并使用伪元素显示三角形即可。 Demo: 演示:

 .left-menu { width: 250px; } .left-menu-item { position: relative; /* height to fit triangle to the right */ height: 66px; /* styles for centering text */ display: flex; /* center vertically */ align-items: center; /* center horizontally */ justify-content: center; /* just styles for demo */ background-color: #066d30; color: #fff; font-weight: bold; } /* change background-color on hover */ .left-menu-item:hover { background-color: #1d8631; } /* show triangle on hover */ .left-menu-item:hover:after { content: ""; position: absolute; left: 100%; top: 0; border-left: 33px solid #1d8631; border-top: 33px solid transparent; border-bottom: 33px solid transparent; } 
 <div class="left-menu"> <div class="left-menu-item">Ottogi</div> <div class="left-menu-item">Sayou Hapyo</div> <div class="left-menu-item">Natura Bogata</div> <div class="left-menu-item">TOO Many</div> </div> 

You've done most of the work already. 您已经完成了大部分工作。 You just need to change the CSS selectors #base and #base:before to .shadow-effect:hover and .shadow-effect:hover:before . 您只需要将CSS选择器#base#base:before更改为.shadow-effect:hover.shadow-effect:hover:before This means that now the effect will be applied to your .shadow-effect div's when user hovers on them. 这意味着,当用户将鼠标悬停在它们上时,该效果现在将应用于您的.shadow-effect div。

Also, since you have given some height, width and margin to the hover design, I added them to the .shadow-effect as well. 另外,由于您已经为悬停设计提供了一些高度,宽度和边距,因此我也将它们添加到了.shadow-effect中。 So when you need to modify them, change them in both classes. 因此,当您需要修改它们时,请在两个类中进行更改。 Else, ideally saying, you should remove those styles from .shadow-effect:hover styles. 否则,理想情况下,您应该从.shadow-effect:hover样式中删除这些样式。

 .shadow-effect { height: 66px; margin-top: 20px; margin-right: 55px; width: 500px; } .shadow-effect:hover { background: #0a863d; display: inline-block; height: 66px; margin-top: 20px; margin-right: 55px; position: relative; width: 500px; } .shadow-effect:hover:before { border-left: 35px solid #0a863d; border-top: 33px solid transparent; border-bottom: 33px solid transparent; content: ""; height: 0; top: 0; position: absolute; right: -35px; width: 0; } 
 <div class="left-main col-lg-3 col-md-3 col-sm-12 col-xs-12"> <div class="shadow-effect"><p class="ottogi">OTTOGI</p></div> <div class="shadow-effect"><p class="sajo">Sajo Hapyo</p></div> <div class="shadow-effect"><p class="natura">Natura Bogata</p></div> <div class="shadow-effect"><p class="maloo">ТОО Малу</p></div> <div class="shadow-effect"><p class="dongush">Dongsuh</p></div> <div class="shadow-effect"><p class="may">ООО Май</p></div> </div> 

Just removed left border in the before , than add it again in hover plus added an box shadow so it would look like the exemple 只是删除了before left border ,而不是再次将其添加到hover并添加了一个框阴影,因此它看起来像一个示例

 .shadow-effect { display: inline-block; height: 66px; position: relative; width: 500px; text-align:center; } p{ line-height:2em; color:#fff; } .container{ padding-top:10px; background-color:#055f2a; width: 500px; } .shadow-effect:hover{ -webkit-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75); -moz-box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75); box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.75); background: #0a863d; } .shadow-effect:hover:before { border-left: 35px solid #0a863d; } .shadow-effect:before { border-top: 33px solid transparent; border-bottom: 33px solid transparent; content: ""; height: 0; top: 0; position: absolute; right: -35px; width: 0; } 
 <div class="left-main col-lg-3 col-md-3 col-sm-12 col-xs-12 container"> <div class="shadow-effect"><p class="ottogi">OTTOGI</p></div> <div class="shadow-effect"><p class="sajo">Sajo Hapyo</p></div> <div class="shadow-effect"><p class="natura">Natura Bogata</p></div> <div class="shadow-effect"><p class="maloo">ТОО Малу</p></div> <div class="shadow-effect"><p class="dongush">Dongsuh</p></div> <div class="shadow-effect"><p class="may">ООО Май</p></div> </div> 

You can achieve that only with css, check this out 您只能使用CSS来实现,请检查一下

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

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