ForeverNewbie
ForeverNewbie

Reputation: 11

css clip-path hover only on shape

as seen in my codepen, there are two greyscaled shapes. Now its only possible to hover one of both, because the original size is a box and overlaps both images. But is there a way to like recognize the shape when hovering it? Z-index does not lead to a result... Hope you can help me out a bit! would be great without JS but its not a k.o.

html:

<div id="one">
<img src="https://cdn.pixabay.com/photo/2019/01/19/15/53/ice- 
3941906_1280.jpg">
 </div>
<div id="two">
<img src="https://cdn.pixabay.com/photo/2016/04/20/17/02/tuscany- 
1341536_1280.jpg">
</div>

css:

img{
  width: 700px;
  height: 400px;
  object-fit: cover;
}

#one img{
  -webkit-clip-path: polygon(0 0, 0% 100%, 100% 0);
clip-path: polygon(0 0, 0% 100%, 100% 0);
}

#two img{
  -webkit-clip-path: polygon(100% 100%, 0% 100%, 100% 0);
clip-path: polygon(100% 100%, 0% 100%, 100% 0);
}

#one{
  position: absolute;
  z-index: 0;
  top: 0 ;
  left: 0;
}

#two{
position:absolute;
  top: 0px;
  left: 0;
  z-index: 0;
}

#one,
#two{
  filter:grayscale(100%);
}

#one:hover,
#two:hover{
  filter:grayscale(0%);
}

https://codepen.io/robwe30/pen/eXBvzp?editors=1100

Cheers

Upvotes: 0

Views: 2198

Answers (2)

Temani Afif
Temani Afif

Reputation: 273530

Here is another idea to create the same using less of code and without clip-path

.container {
  width: 300px;
  height: 200px;
  position: relative;
  overflow: hidden;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.container div {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform: skewX(-56.3deg); /* tan(angle) = Width/height --> angle = arctan(width/height) */
  transform-origin: top;
  overflow: hidden;
}

.container div img {
  transform: skewX(56.3deg);
  transform-origin: top;
}
.container img:hover{
   filter:grayscale(100%);
}
<div class="container">
  <img src="https://picsum.photos/800/600?image=1069">
  <div>
    <img src="https://picsum.photos/800/600?image=1051">
  </div>
</div>

Upvotes: 1

G-Cyrillus
G-Cyrillus

Reputation: 106008

For that simple shape, You may play with pointer-events to remove it and a pseudo element rotated to switch it back on only on one part of the image.

about pointer-events https://css-tricks.com/almanac/properties/p/pointer-events/

example

/* CSS switch of  pointer-events  on/off */

#two {
  position: relative;
  overflow: hidden;/* hide pseudo overflowing */
  pointer-events: none;
}

#two:before {
  pointer-events: auto;
  content: '';
  position: absolute;
  top: 100%;
  left: 0;
  z-index: 2;
  width: 150%;
  height: 100%;
  
  /* set transform according to ratio image */
  transform-origin: top left;
  transform: rotate(-30deg);
  
  /* if you want to see where it stands , add borders or background */
}


/* end CSS  switch */

img {
  width: 700px;
  height: 400px;
  object-fit: cover;
  display: block;/* avoids gap underneath */
}

#one img {
  -webkit-clip-path: polygon(0 0, 0% 100%, 100% 0);
  clip-path: polygon(0 0, 0% 100%, 100% 0);
}

#two img {
  -webkit-clip-path: polygon(100% 100%, 0% 100%, 100% 0);
  clip-path: polygon(100% 100%, 0% 100%, 100% 0);
}

#one {
  position: absolute;
  z-index: 0;
  top: 0;
  left: 0;
}

#two {
  position: absolute;
  top: 0px;
  left: 0;
  z-index: 0;
}

#one,
#two {
  filter: sepia(100%);/* demo purpose */
}

#one:hover,
#two:hover {
  filter: grayscale(0%);
}
<div id="one">
  <img src="https://picsum.photos/800/600?image=1060">
</div>
<div id="two">
  <img src="https://picsum.photos/800/600?image=1065">
</div>

https://codepen.io/gc-nomade/pen/aMBwYz

Upvotes: 1

Related Questions