Reputation: 227
I'm trying to fill a button with a diagonal effect on the mousemove (so the button is filled where the mouse is)
I have the effect of a diagonal fill on a hover:
.demo {
text-align: center;
margin-top: 150px;
}
.mt {
margin-top: 20px;
}
.small {
width: 120px;
}
.medium {
width: 160px;
}
.large {
width: 230px;
}
.extra-large {
width: 360px;
}
.diagonal {
position: relative;
display: inline-block;
line-height: 50px;
border: 1px solid black;
color: black;
background: white;
font-weight: book;
font-family: 'Open Sans', sans-serif;
overflow: hidden;
z-index: 1;
padding: 0px;
}
.diagonal:after {
content: "";
position: absolute;
top: 30;
left: 0;
width: 500%;
height: 1000%;
background: #F5FF35;
z-index: -1;
transform-origin: 0% 0%;
transform: translateX(calc(-130% - 0px)) translateY(10%) rotate(45deg);
transition: transform 1s;
}
.diagonal:hover::after {
transform: translateY(-100%) translateX(-50px) rotate(45deg);
direction: ltr;
}
<div class="demo">
<a class="mt small diagonal">Click me!</a><br>
<button class="mt medium diagonal">Click me!</button><br>
<button class="mt large diagonal">Click me!</button><br>
<button id="demo" class="mt extra-large diagonal">Click me!</button>
</div>
But I can't set it up on a mousemove, I have found something similar from an other question, fiddle.
Basically, I am trying to fit the codepen and the jsfiddle together
I tried to rotate the div in the jsfiddle but the fill effect would be all over instead of only the start, plus it would not cover the whole area..
Upvotes: 2
Views: 1228
Reputation: 22949
You could do this by mostly adjusting the CSS, basically rotating that green div.
var parent = document.getElementById('parent');
var child = document.getElementById('child');
parent.addEventListener("mousemove", fill);
parent.addEventListener("mouseleave", hasLeft);
function fill(e) {
if (e.x <= parent.offsetWidth) {
child.style.width = e.x + 'px';
}
};
function hasLeft(e) {
if (child.offsetWidth === parent.offsetWidth) {
} else {
child.style.width = '0px';
}
}
.wrapper {
overflow: hidden;
height: 60px;
width: 100px;
background-color: #FFFF00;
}
#parent {
height: 65px;
width: 100px;
transform: scale(2) rotate(-45deg);
}
#child {
height: 65px;
width: 0;
background-color: #00FF00;
}
<div class="wrapper">
<div class='orange' id='parent'>
<div class='green' id='child'>
</div>
</div>
</div>
Upvotes: 2
Reputation: 32354
You can try something like this:
$('.green').on('mousemove', function(e) {
if (e.pageX < $(this).width());
var percent = e.pageX - $(this).offset().left * 100 / $(this).width();
$(this).css({
'background': 'linear-gradient(45deg, rgba(0,0,0,1) 0%,rgba(0,0,0,1) ' + percent + '%,rgba(0,0,0,0) ' + (percent + 0.1) + '%,rgba(0,0,0,0) 100%)'
});
});
demo:https://jsfiddle.net/sdq5z7ej/4/
Upvotes: 3