【发布时间】:2016-03-07 22:55:18
【问题描述】:
我用 jQuery 制作了一个自定义悬停效果。但它并不总是那么顺利。我想经常在多个按钮和 div 上使用它。我不是 jQuery 的专业人士,所以我认为这段代码可以更高效、更干净。
它的作用是创建两个 div,它们通过 CSS(剪辑路径)转换为半个正方形(三角形)。这两个三角形获得您悬停的元素的outerWidth() 和outherHeight()。三角形位于悬停元素之外,因此它们可以滑入。当您将鼠标悬停时,三角形将滑出然后被删除。然后outerWidth() 和outerHeight() 将被重置。
我已经把它放在了 jsfiddle 上。
https://jsfiddle.net/6uuh0ha7/5/
HTML
<div class="effect position">
CSS
.position{
left: 100px;
top: 100px;
width: 100px;
height: 100px;
border: solid 1px;
}
.effect .first {
opacity: 0;
width: 100%;
height: 100%;
background-size: cover;
-webkit-clip-path: polygon(0 0, 0 100%, 100% 100%);
clip-path: polygon(0 0, 0% 100%, 100% 100%);
}
.effect:hover .first {
width: 100%;
height: 100%;
background-size: cover;
-webkit-clip-path: polygon(0 0, 0 100%, 100% 100%);
clip-path: polygon(0 0, 0% 100%, 100% 100%);
}
.effect .second {
opacity: 0;
width: 100%;
height: 100%;
background-size: cover;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%);
clip-path: polygon(0 0, 100% 0, 100% 100%);
}
.effect:hover .second {
/*opacity: 1;*/
width: 100%;
height: 100%;
background-size: cover;
-webkit-clip-path: polygon(0 0, 100% 0, 100% 100%);
clip-path: polygon(0 0, 100% 0, 100% 100%);
}
.effect {
overflow: hidden;
position: relative;
text-align: center;
cursor: default;
}
.effect .mask{
position: absolute;
overflow: hidden;
top: 0;
left: 0;
}
jQuery
jQuery( document ).ready(function( $ ) {
$( ".effect" ).hover( function() {
$(".first", this).finish();
$(".second", this).finish();
$colorRed = "#ff0000";
$colorDarkRed = "#930000";
$(this).append('<div class="mask first temp" style="background-color:' + $colorRed + ';"></div>');
$(this).append('<div class="mask second temp" style="background-color:' + $colorDarkRed + ';"></div>');
$width = $(this).outerWidth() + 'px';
$height = $(this).outerHeight() + 'px';
$( ".first", this ).css({"left" : $width, "top" : $height});
$( ".second", this ).css({"left" : "-" + $width, "top" : "-" + $height});
$( ".first", this ).css({"opacity" : "1"});
$( ".second", this ).css({"opacity" : "1"});
$( ".first", this ).filter(':not(:animated)').animate({
top: "-0",
left: "-0"
}, 200, function() {
// Animation complete.
});
$( ".second", this ).filter(':not(:animated)').animate({
top: "+0",
left: "+0"
}, 200, function() {
// Animation complete.
});
},
function() {
$( ".first", this ).animate({
top: "+" + $height,
left: "+" + $width
}, 200, function() {
$( ".first", this ).css({"opacity" : "0"});
});
$( ".second", this ).animate({
top: "-" + $height,
left: "-" + $width
}, 200, function() {
$( ".second", this ).css({"opacity" : "0"});
});
$('.temp').animate({opacity: 0 },
'fast', // how fast we are animating
'linear', // the type of easing
function() { // the callback
$(this).remove();
});
$width = undefined;
$height = undefined;
});
});
提前谢谢你。
【问题讨论】:
-
jsfiddle 什么都不做。只有白色
-
“优化”是您可以在CodeReviewe 获得帮助的内容,但在发布之前请查看他们的指导方针。这个问题对 SO 来说是题外话。
-
对不起,我以为我在上传这个问题之前检查了它,但出了点问题。我已经用 // 放入了 CSS cmets。一个错误。我现在把它修好了。
标签: jquery html css optimization hover