A smooth background wipe effect can be created using CSS pseudo-elements and transitions.
First inject a background pseudo-element into your main container. Then when a class is added to the container, use a transform to move that background out of the way.
#slide:before {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
content: "";
transition: transform 1s ease-in-out;
background: teal;
}
#slide.on:before {
transform: translateX(100%);
}
$("#slide").on("click", function() {
$(this).toggleClass("on");
});
#slide {
position: relative;
overflow: hidden;
width: 200px;
height: 100px;
background: salmon;
}
#slide .contents {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
}
#slide:before {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
content: "";
transition: transform 1s ease-in-out;
background: teal;
}
#slide.on:before {
transform: translateX(100%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide">
<div class="contents">Click me!</div>
</div>
A few small considerations:
- The container's rules should include
overflow: hidden
and position: relative
.
- Pseudo-elements will stack on top of non-positioned content nodes, so any visible contents of the container will need to be wrapped in a second block element.
Example on Codepen