Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Запрет повторной анимации (https://javascript.ru/forum/dom-window/61501-zapret-povtornojj-animacii.html)

s24344 21.02.2016 08:51

Запрет повторной анимации
 
Здравствуйте уважаемы форумчане. Подскажите пожалуйста как запретить повторную анимацию при многократном наведении мыши. Прилагаю пример:

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
	<link rel="stylesheet" href="css/normalize.css">
	<style>
		.wrapper{
			max-width: 1278px;
			margin: 40px auto;
			line-height: 140%;
		}
		.clearfix:before, .clearfix:after{
			content: " ";
			display: table;
		}
		.clearfix:after{
			clear: both;
		}
		.hover {
			float: left;
		}
		.show {
			float: left;
			width: 400px;
			background-color: #f6f6f6;
			padding: 10px;
			float: left;
			margin-left: 40px;
			display: none;
		}
	</style>
</head>
<body>
	<div class="wrapper clearfix">
		<div class="hover"><a class="hover__link" href="#">Lorem ipsum</a></div>
		<div class="show"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum excepturi voluptatem expedita ipsa totam tenetur corporis at. Officia voluptates impedit sed, dicta laborum aliquid aut dolorem natus. A, veritatis alias.</p></div>
	</div>
	
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>


$(function() {

	var flag = true;

	var timer;

	var show = function(elements, el) {

		$(elements).on('mouseenter', function() {
			clearTimeout(timer);
			$(el).fadeIn(300);
		})

		$(elements).on('mouseleave', function() {
			timer = setTimeout(function() { 
				$(el).fadeOut(300);
			},100)
		});
	}

	show(".hover, .show", ".show");
});

рони 21.02.2016 08:59

s24344,
<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>
	<link rel="stylesheet" href="css/normalize.css">
	<style>
		.wrapper{
			max-width: 1278px;
			margin: 40px auto;
			line-height: 140%;
		}
		.clearfix:before, .clearfix:after{
			content: " ";
			display: table;
		}
		.clearfix:after{
			clear: both;
		}
		.hover {
			float: left;
		}
		.show {
			float: left;
			width: 400px;
			background-color: #f6f6f6;
			padding: 10px;
			float: left;
			margin-left: 40px;
			display: none;
		}
	</style>
</head>
<body>
	<div class="wrapper clearfix">
		<div class="hover"><a class="hover__link" href="#">Lorem ipsum</a></div>
		<div class="show"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum excepturi voluptatem expedita ipsa totam tenetur corporis at. Officia voluptates impedit sed, dicta laborum aliquid aut dolorem natus. A, veritatis alias.</p></div>
	</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script>$(function() {



	var show = function(elements, el) {

		$(elements).on('mouseenter', function() {

			$(el).stop(true,true).fadeIn(300);
		})

		$(elements).on('mouseleave', function() {

				$(el).stop().delay(300).fadeOut(300);

		});
	}

	show(".hover, .show", ".show");
});
</script>
</body>
</html>

s24344 21.02.2016 09:29

Спасибо! А как сделать тоже самое, но на js (без библиотек)?

рони 21.02.2016 09:49

s24344,
написать свои mouseenter и mouseleave fadeIn fadeOut
https://learn.javascript.ru/mousemov...ter-mouseleave
https://learn.javascript.ru/js-animation
этих двух ссылок достаточно, чтобы это осуществить ... хотя можно придумать вариант без js на одном css.

s24344 21.02.2016 09:58

Спасибо.

s24344 21.02.2016 10:42

Последний вопрос: как реализовать .stop(true, true) ?

рони 21.02.2016 11:58

fadeIn fadeOut на чистом js
 
s24344,
<!doctype html>
<html>
<head>

<style>

	.wrapper{
			max-width: 1278px;
			margin: 40px auto;
			line-height: 140%;
		}
		.clearfix:before, .clearfix:after{
			content: " ";
			display: table;
		}
		.clearfix:after{
			clear: both;
		}
		.hover {
			float: left;
		}
		.show {
			float: left;
			width: 400px;
			background-color: #f6f6f6;
			padding: 10px;
			float: left;
			margin-left: 40px;
			display: none;
		}


</style>

</head>

<body>

<div class="wrapper clearfix">
		<div class="hover"><a class="hover__link" href="#">Lorem ipsum</a></div>
		<div class="show"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eum excepturi voluptatem expedita ipsa totam tenetur corporis at. Officia voluptates impedit sed, dicta laborum aliquid aut dolorem natus. A, veritatis alias.</p></div>
	</div>


<script>


window.requestAnimationFrame = window.requestAnimationFrame
                               || window.mozRequestAnimationFrame
                               || window.webkitRequestAnimationFrame
                               || window.msRequestAnimationFrame
                               || function(f){return setTimeout(f, 1000/60)}

window.cancelAnimationFrame = window.cancelAnimationFrame
                              || window.mozCancelAnimationFrame
                              || function(requestID){clearTimeout(requestID)} //fall back


var requestframeref;
var hover = document.querySelector('.hover');
var show = document.querySelector('.show');
var opacity = 0;
var timestart;
function fadeIn(){
		opacity += 2
		if (opacity > 100){
			opacity = 100;
		}

	else{
    requestframeref = requestAnimationFrame(fadeIn)
	}
	show.style.opacity = opacity/100;

}
function fadeOut(timestamp){
		if (timestamp - timestart > 300)opacity -= 3;
		if (opacity < 0){
			opacity = 0;
            show.style.display = 'none'
		}

	else{
    requestframeref = requestAnimationFrame(fadeOut)
	}
	show.style.opacity = opacity/100;

}

hover.addEventListener('mouseenter', function(){
    cancelAnimationFrame(requestframeref);
    show.style.display = 'block'
	requestAnimationFrame(fadeIn)
}, false)
show.addEventListener('mouseenter', function(){
    cancelAnimationFrame(requestframeref);
    show.style.display = 'block'
	requestAnimationFrame(fadeIn)
}, false)

hover.addEventListener('mouseleave', function(){
	cancelAnimationFrame(requestframeref);
    timestart = performance.now();
    requestAnimationFrame(fadeOut)
}, false)
show.addEventListener('mouseleave', function(){
	cancelAnimationFrame(requestframeref);
    timestart = performance.now();
    requestAnimationFrame(fadeOut)
}, false)

</script>

</body>
</html>

s24344 21.02.2016 15:19

Спасибо


Часовой пояс GMT +3, время: 05:09.