需要帮助改善扑克牌悬停效果

时间:2016-08-12 08:25:03

标签: javascript jquery css html5 animation

当卡片通过另一张卡片时,我对z-index动画有一些问题。 你有什么想法改进动画并让它更流畅吗? 这里是动画:https://jsfiddle.net/mk51gs1t/1/ THX

css:

.card {
display          : inline-block;
position        : relative;
width            : 100px;
height           : 200px;
margin-left      : -20px;
margin-right     : -20px;

/*background-color : #999999;*/
background-image:linear-gradient(90deg, #999999, #666666);
border           : 3px solid #2e6da4;

z-index: 1;

transition: all .2s ease-in-out;
}

.center{
    transform: scale(1.2, 1.2);
}

.previous{
    transform: scale(1.1, 1.1);
}

.next{
    transform: scale(1.1, 1.1);
}

js:

    $(function () {
    zIndexManager($('.center'), $('.previous'), $('.next'));
    var cardDOMArray = $('.card');
    //cardDOMArray.hover(cardEnter, cardLeave);
    cardDOMArray.hover(function (e) {
        cardEnter(e.currentTarget, cardDOMArray);
    }, function (e) {
        cardLeave(e.currentTarget, cardDOMArray);
    });

    cardDOMArray.on("click", function (e) {
        drawCard(e.currentTarget);
    });

});

function cardEnter(e, cardDOMArray) {
    //  console.log("enter");
    //  console.log(getPreviousCard($(e)));
    //  console.log(getNextCard($(e)));

    $('.card').removeClass("center previous next");

    getPreviousCard($(e)).addClass("previous");
    getNextCard($(e)).addClass("next");
    $(e).addClass("center");
    zIndexManager($(e), getPreviousCard($(e)), getNextCard($(e)));
}

function cardLeave(e, cardDOMArray) {

    $('.card').removeClass("center previous next").css('zIndex', '1');
}

function getPreviousCard(card) {
    if (card.prev().length > 0) {
        return card.prev();
    }
    else {
        return $();
    }
}

function getNextCard(card) {
    if (card.next().length > 0) {
        return card.next();
    }
    else {
        return $();
    }
}

function zIndexManager(centerCard, previousCard, nextCard) {
    var allNextCard       = centerCard.nextAll();
    //console.log(allNextCard );
    var allNextCardLength = allNextCard.length;
    //console.log(allNextCardLength);
    if (allNextCardLength > 0) {
        centerCard.css('zIndex', allNextCardLength);
        allNextCard.each(function () {
            $(this).css('zIndex', --allNextCardLength);
        });
    }
}

html:

<div class="container">
    <h1>carte</h1>
    <div class="container cardWidget">
        <div class="card ">
        </div>
        <div class="card ">
        </div>
        <div class="card">
        </div>
        <div class="card ">
        </div>
        <div class="card previous">
        </div>
        <div class="card center">
        </div>
        <div class="card next">
        </div>
        <div class="card">
        </div>
        <div class="card">
        </div>
        <div class="card">
        </div>
        <div class="card">
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

如何不动画你的z-index变化:

transition: transform .25s ease-out;

对我来说看起来很顺利。

JSFiddle

$(function () {
	zIndexManager($('.center'), $('.previous'), $('.next'))
	var cardDOMArray = $('.card');
	//cardDOMArray.hover(cardEnter, cardLeave);
	cardDOMArray.hover(function (e) {
		cardEnter(e.currentTarget, cardDOMArray);
	}, function (e) {
		cardLeave(e.currentTarget, cardDOMArray);
	});

	cardDOMArray.on("click", function (e) {
		drawCard(e.currentTarget);
	});

});

function cardEnter(e, cardDOMArray) {
	// 	console.log("enter");
	// 	console.log(getPreviousCard($(e)));
	// 	console.log(getNextCard($(e)));

	zIndexManager($(e), getPreviousCard($(e)), getNextCard($(e)));
	$('.card').removeClass("center previous next");

	getPreviousCard($(e)).addClass("previous");
	getNextCard($(e)).addClass("next");
	$(e).addClass("center");
}

function cardLeave(e, cardDOMArray) {

	$('.card').removeClass("center previous next").css('zIndex', '1');
}

function getPreviousCard(card) {
	if (card.prev().length > 0) {
		return card.prev();
	}
	else {
		return $();
	}
}

function getNextCard(card) {
	if (card.next().length > 0) {
		return card.next();
	}
	else {
		return $();
	}
}

function zIndexManager(centerCard, previousCard, nextCard) {
	var allNextCard       = centerCard.nextAll();
	//console.log(allNextCard );
	var allNextCardLength = allNextCard.length;
	//console.log(allNextCardLength);
	if (allNextCardLength > 0) {
		centerCard.css('zIndex', allNextCardLength);
		allNextCard.each(function () {
			$(this).css('zIndex', --allNextCardLength);
		});
	}
}
.card {
	display          : inline-block;
	position        : relative;
	width            : 100px;
	height           : 200px;
	margin-left      : -20px;
	margin-right     : -20px;

	/*background-color : #999999;*/
	background-image:linear-gradient(90deg, #999999, #666666);
	border           : 3px solid #2e6da4;

	z-index: 1;

	transition: transform .25s ease-out;
}

.center{
	transform: scale(1.2, 1.2);
}

.previous{
	transform: scale(1.1, 1.1);
}

.next{
	transform: scale(1.1, 1.1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
	<h1>carte</h1>
	<div class="container cardWidget">
		<div class="card ">
		</div>
		<div class="card ">
		</div>
		<div class="card">
		</div>
		<div class="card ">
		</div>
		<div class="card previous">
		</div>
		<div class="card center">
		</div>
		<div class="card next">
		</div>
		<div class="card">
		</div>
		<div class="card">
		</div>
		<div class="card">
		</div>
		<div class="card">
		</div>
	</div>
</div>