Reputation: 169
I have a element with css clip-path.
Depending on the cursor position (Xcord) jquery
modify clip-path
points.
My code works great, but I want to slow down (slow and smooth) this "animation" even if the cursor moves quickly.
how can I achieve that? thx for help
$(document).mousemove(function(getCurrentPos){
var clip = $(".element");
//x coordinates
var xCord = getCurrentPos.pageX;
//calculate %
xPercent = xCord / $(document).width() * 100;
var left = 90 + 10 * (xPercent / 100);
var right = 100 - 10 * (xPercent / 100);
$(".element").css('clip-path', 'polygon(0% 0%, 100% 0%, 100% ' + left + '%, 0% ' + right + '%)');
});
.element {
background:red;
width:500px;
height:150px;
clip-path:polygon(0% 0%, 100% 0%, 100% 100%, 0% 90%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="element"></div>
Upvotes: 0
Views: 815
Reputation: 273561
Simply add a transition on the CSS code
$(document).mousemove(function(getCurrentPos) {
var clip = $(".element");
//x coordinates
var xCord = getCurrentPos.pageX;
//calculate %
xPercent = xCord / $(document).width() * 100;
var left = 90 + 10 * (xPercent / 100);
var right = 100 - 10 * (xPercent / 100);
$(".element").css('clip-path', 'polygon(0% 0%, 100% 0%, 100% ' + left + '%, 0% ' + right + '%)');
});
.element {
background: red;
width: 500px;
height: 150px;
clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 90%);
transition:0.5s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="element"></div>
Upvotes: 1