css3鼠標拖拽卡片動畫特效



75 296 99



特效描述:css3鼠標拖拽 卡片動畫特效,css3鼠標拖拽卡片動畫特效

代碼結構

1. 引入CSS

<link rel="stylesheet" href="css/style.css">

2. HTML代碼

<div class="card">
	<img src="img/hearthstone-ragnaros.png" class="card-image" draggable="false">
</div>
<script type="text/javascript">
	var card,
		image,
		cardw,
		cardh,
		cardx,
		cardy,
		ocardx,
		ocardy,
		pinx,
		piny,
		pinxperc,
		pinyperc,
		targetx,
		targety,
		rx,
		ry,
		targetrx,
		targetry,
		scale,
		targetscale,
		ww,
		wh,
		md,
		mx,
		my,
		whoosh,
		whooshvol,
		whooshvoltarget,
		majesty,
		majestyvol,
		majestyvoltarget,
		audioloaded;
	function audioload() {
		audioloaded++;
		if( audioloaded == 2 ) {
			document.body.classList.add( 'loaded' );
			majesty.play();
			whoosh.play();
			bindevents();
			loop();
		}
	}
	function init() {
		onresize();
		card = document.querySelector( '.card' );
		image = document.querySelector( '.card-image' );
		cardw = image.width;
		cardh = image.height;
		cardx = ww / 2 - cardw / 2;
		cardy = wh / 2 - cardh / 2;
		ocardx = cardx;
		ocardy = cardy;
		pinx = 0;
		piny = 0;
		pinxperc = 0;
		pinyperc = 0;
		targetx = cardx;
		targety = cardy;
		rx = 0;
		ry = 0;
		targetrx = 0;
		targetry = 0;
		scale = 1;
		targetscale = scale;
		md = false;
		mx = cardx;
		my = cardy;
		audioloaded = 0;
		whooshvol = 0;
		whooshvoltarget = 0;
		whoosh = new Audio();
		whoosh.addEventListener( 'canplaythrough', audioload );
		whoosh.src = 'media/hs-whoosh.ogg';
		whoosh.volume = 0;
		whoosh.loop = true;
		majestyvol = 0;
		majestyvoltarget = 0;
		majesty = new Audio();
		majesty.addEventListener( 'canplaythrough', audioload );
		majesty.src = 'media/hs-majesty.ogg';
		majesty.volume = 0;
		majesty.loop = true;
	}
	function bindevents() {
		card.addEventListener( 'mousedown', onmousedown );
		window.addEventListener( 'mouseup', onmouseup );
		window.addEventListener( 'mousemove', onmousemove );
		window.addEventListener( 'resize', onresize );
	}
	function onmousedown( e ) {
		md = true;
		mx = e.pageX;
		my = e.pageY;
		pinx = cardw / 2;
		piny = cardh / 2;
		//pinx = mx - cardx; // to pin to click point
		//piny = my - cardy; // to pin to click point
		pinxperc = 100 - ( pinx / cardw ) * 100;
		pinyperc = 100 - ( piny / cardh ) * 100;
	}
	function onmouseup() {
		md = false;
	}
	function onmousemove( e ) {
		if( md ) {
			mx = e.pageX;
			my = e.pageY;
		}
	}
	function onresize() {
		ww = window.innerWidth;
		wh = window.innerHeight;
	}
	function loop() {
		requestAnimationFrame( loop )
		targetx = mx - cardx - pinx;
		targety = my - cardy - piny;
		cardx += targetx * 0.25;
		cardy += targety * 0.25;
		if( cardx < -cardw / 2 ) {
			cardx = -cardw / 2;
		}
		if( cardx > ww - cardw / 2 ) {
			cardx = ww - cardw / 2;
		}
		if( cardy < -cardh / 2 ) {
			cardy = -cardh / 2;
		}
		if( cardy > wh - cardh / 2 ) {
			cardy = wh - cardh / 2;
		}
		targetrx = ( ocardy - cardy - rx ) * 3;
		targetry = ( cardx - ocardx - ry ) * 3;
		targetrx = Math.min( targetrx, 90 );
		targetrx = Math.max( targetrx, -90 );
		targetry = Math.min( targetry, 90 );
		targetry = Math.max( targetry, -90 );
		rx += targetrx * 0.1;
		ry += targetry * 0.1;
		targetscale = md ? 1.2 - scale : 1 - scale;
		scale += targetscale * 0.2;
		card.style[ 'transform' ] = 'translate3d(' + cardx + 'px, ' + cardy + 'px, 0)';
		image.style[ 'transform-origin' ] = pinxperc + '% ' + pinyperc + '%';
		image.style[ 'transform' ] = 'scale(' + scale + ') rotateY(' + ry + 'deg) rotateX(' + rx + 'deg)';
		majestyvoltarget = md ? 0.2 : 0;
		majestyvol += ( majestyvoltarget - majestyvol ) * 0.1;
		majesty.volume = majestyvol;
		whooshvoltarget = ( Math.abs( ( ocardy - cardy ) ) + Math.abs( ( ocardx - cardx ) ) ) * 0.003;
		whooshvol += ( whooshvoltarget - whooshvol ) * 0.1;
		whoosh.volume = Math.min( whooshvol, 1 );
		ocardx = cardx;
		ocardy = cardy;
	}
	window.onload = init;
</script>



用戶評論
大牛,別默默的看了,快登錄幫我點評一下吧!:)      登錄 | 注冊


熱門標簽: 拖動 拖拽 拖動插件 拖拽插件 圖片拖動 圖片拖拽
?
×
×

注冊

官方QQ群

掃描上面二維碼加微信群

官方QQ群

jQuery/js討論群
群號:642649996
Css3+Html5討論群
群號:322131262

加群請備注:從官網了解到

老夫子电子