Skip to main content
Inspiring
May 18, 2017
Answered

Complex Hover Effect

  • May 18, 2017
  • 3 replies
  • 1890 views

The banner image has a centered call to action (CTA) button. The CTA has a transparent background reveling the banner image. Using css I would like to change the banner image to black & white (BW). The area behind the CTA should remain in color and no affected by the banner img BW effect. Next, hovering on the CTA buttons removes the BW effect on the banner image. Can this be done with css?

http://photoshopace.com/snipets/banner-box-hover-v1.html

default banner state

banner in color on mouse hover CTA

This topic has been closed for replies.
Correct answer osgood_

This is as close as I can get it. Seems to work in Firefox, Chrome - Safari has issues but it still works albeit not 100%. Don't know about anything else. It uses the clip: rect() css method, now superceeded by clip-path but that don't work in much at the moment. Is it worth the effort, no.

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8" />

<title>Overlay Background</title>

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<script>

$(document).ready(function(){

$('.overlay a').css('cursor' , 'pointer').mouseenter(function(){

$('.background_image').css({'-webkit-filter': 'none', 'filter': 'none'});

});

$('.overlay a').mouseout(function(){

$('.background_image').css({'-webkit-filter': 'grayscale(100%)', 'filter': 'grayscale(100%)'});

});

});

$(document).ready(function(){

var container_width = $(".container").css("width").replace(/[^-\d\.]/g, '');

var overlay_width = $(".overlay a").css("width").replace(/[^-\d\.]/g, '');

var y = container_width - overlay_width;

var y = y / 2;

var newwidth = + overlay_width + y;

if ($(window).width() > 481) {

$(".overlay").css('clip' , 'rect(185px,' + newwidth + 'px, 250px,' + y + 'px');

}

if ($(window).width() < 480) {

$(".overlay").css('clip' , 'rect(172px,' + newwidth + 'px, 250px,' + y + 'px');

}

});

$(document).ready(function(){

$(window).resize(function() {

var container_width = $(".container").css("width").replace(/[^-\d\.]/g, '');

var overlay_width = $(".overlay a").css("width").replace(/[^-\d\.]/g, '');

var y = container_width - overlay_width;

var y = y / 2;

var newwidth = + overlay_width + y;

if ($(window).width() > 481) {

$(".overlay").css('clip' , 'rect(185px,' + newwidth + 'px, 250px,' + y + 'px');

}

if ($(window).width() < 480) {

$(".overlay").css('clip' , 'rect(172px,' + newwidth + 'px, 250px,' + y + 'px');

}

});

});

</script>

<style>

* {

box-sizing: border-box;

}

.container {

position: relative;

height: 300px;

width: 85%;

margin: 0 auto;

max-width: 1200px;

}

@media screen and (max-width: 900px) {

.container {

width: 95%;

}

}

.background_image {

position: relative;

background-image: url(http://lorempixel.com/1200/400/nature/6/);

background-position: center center;

-webkit-filter: grayscale(1);

transition: opacity .3s ease-in-out;

-moz-transition: opacity .3s ease-in-out;

-webkit-transition: opacity .3s ease-in-out;

height: 300px;

}

.background_image:hover {

transition: opacity .3s ease-in-out;

-moz-transition: opacity .3s ease-in-out;

-webkit-transition: opacity .3s ease-in-out;

}

.overlay {

position: absolute;

z-index: 1000;

width: 100%;

height: 100%;

background-image: url(http://lorempixel.com/1200/400/nature/6/);

background-position: center center;

-webkit-filter: grayscale(0);

clip: rect(185px, auto ,250px, auto);

}

@media screen and (max-width: 480px) {

.overlay {

clip: rect(165px, 850px ,250px,250px);

}

}

.overlay span {

width: 100%;

position: absolute;

bottom: 50px;

}

.overlay a {

width: 50%;

margin: 0 auto;

display: block;

padding: 18px 0;

text-align: center;

font-family: helvetica, sans-serif;

font-size: 18px;

line-height: 25px;

color: #fff;

border: 1px solid #fff;

}

@media screen and (max-width: 900px) {

.overlay a {

width: 65%;

}

}

@media screen and (max-width: 480px) {

.overlay a {

width: 90%;

padding: 12px 20px;

}

}

</style>

</head>

<body>

<div class="container">

<div class="overlay">

<span><a>Let's talk and see if this is right for you</a></span>

</div>

<!-- end overlay -->

<div class="background_image"></div>

</div>

<!-- end container -->

</body>

</html>

3 replies

pziecina
Legend
May 19, 2017

I'm going to put my reputation on the line here, but depending on which browsers and devices it is important for the OP to support, and why the partial filter effect is required. It should be possible to cut the image in two, then use css grid or flexbox layouts to align the images without any problems.

Jon Fritz
Community Expert
Community Expert
May 19, 2017

Oh, no offense taken.

Like I mentioned, I wasn't sure if it could be done with .js and I knew the second part of your post was for the OP.

Jon Fritz
Community Expert
Community Expert
May 18, 2017

I used vw units to make it responsive, it should work with any image size since the image is resized to 100% of the viewport width...

<!doctype html>

<html lang="en-us">

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Oh, that's better.</title>

<style>

    *{

    -moz-box-sizing:border-box;

    -webkit-box-sizing:border-box;

    box-sizing:border-box;

    margin:0;

    padding:0;

    }

    #banner {

        width:100vw;

        height:30vw;

        background-image:url(images/not-hovered.png);

        background-size:100vw;

    }

    #cta_button {

        position:absolute;

        top:15vw;

        left:0;

        right:0;

        margin:0 auto;

        border:2px solid white;

        width:50vw;

        color:white;

        text-align:center;

        padding:10px;

        background-image:url(images/hovered.png);

        background-repeat:no-repeat;

        background-position:-25vw -15vw;

        background-size:100vw;

        cursor:pointer;

    }

    #cta_button:hover ~ #banner {

        background-image:url(images/hovered.png);

    }

    #wrapper {

        position:relative;

        width:100vw;

    }

    </style>

</head>

<body>

    <div id="wrapper">

        <div id="cta_button">YOUR TEXT HERE</div>

        <div id="banner"></div>

    </div>

</body>

</html>

EDIT: Ooops, you need to have the "hovered" version of the image as the background of the CTA button. Updated the code above.

Inspiring
May 18, 2017

Thanks for all the suggestions and replays. After reviewing the suggestions I have this question. Is it possible to apply a BW filter to part of a div? Somehow programmatically mask part of the div from the filter effect?

Jon Fritz
Community Expert
Community Expert
May 19, 2017

There's nothing in CSS that would allow you to use the black and white filter in that way. I suppose it might be possible using javascript, but I don't know of anything that will do it off hand.

Nancy OShea
Community Expert
Community Expert
May 18, 2017

You need 2 banners -- 1 B&W, 1 full-color. 

The call to action button is more problematic.  You could possibly fudge it with a CSS gradient background sampled from colors in your full-color banner.  See link below.

Ultimate CSS Gradient Generator - ColorZilla.com

Nancy

Nancy O'Shea— Product User & Community Expert
Inspiring
May 18, 2017

I am trying to avoid using the image swap technique because the positioning and alignment of the CTA becomes problematic when it needs to match precisely a specific area on the image. In a responsive environment this is very challenging to make it work.

Perhaps the html image map technique might work. Again, I don't think it will do well in a responsive environment.

Any other ideas?

pziecina
Legend
May 18, 2017

To change a color image to black and white on hover, you could use the css greyscale filter -

https://www.html5rocks.com/en/tutorials/filters/understanding-css/

The main drawback of using a css filter is that it does not work in IE11 or below.