动态心点赞(粘贴即可使用h5代码和vue代码)

1、html文件

<!doctype html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Twitter红心点赞CSS3动画按钮特效</title>
		<style type="text/css">
			
			#container {
				margin: 0 auto;
				width: 900px;
				font-family: arial;
			}

			#container h3 {
				font-family: "Microsoft YaHei";
			}
			.heart {
				background: url('http://product.armark.com.cn/echolac/res/image/Fu3O0aVNZihP-qi7NCH0epcFnju_.png');
				background-position: left;
				background-repeat: no-repeat;
				height: 100px;
				width: 100px;
				cursor: pointer;
				position: absolute;
				left: -14px;
				background-size: 2900%;
			}

			.heart:hover,
			.heart:focus {
				background-position: right;
			}

			@-webkit-keyframes heartBlast {
				0% {
					background-position: left;
				}

				100% {
					background-position: right;
				}
			}

			@keyframes heartBlast {
				0% {
					background-position: left;
				}

				100% {
					background-position: right;
				}
			}

			.heartAnimation {
				display: inline-block;
				-webkit-animation-name: heartBlast;
				animation-name: heartBlast;
				-webkit-animation-duration: .8s;
				animation-duration: .8s;
				-webkit-animation-iteration-count: 1;
				animation-iteration-count: 1;
				-webkit-animation-timing-function: steps(28);
				animation-timing-function: steps(28);
				background-position: right;
			}

			.feed p {
				font-family: "Microsoft YaHei", 'Georgia', Times, Times New Roman, serif;
				font-size: 25px;
			}

			.feed {
				clear: both;
				margin-bottom: :20px;
				height: 150px;
				position: relative;
			}

			.likeCount {
				font-family: 'Georgia', Times, Times New Roman, serif;
				margin-top: 32px;
				margin-left: 68px;
				font-size: 25px;
				color: #999999
			}
		</style>

	</head>
	<body>

		<article class="htmleaf-container">
			<div id="container">
				<h3>点击下面的红心查看效果!</h3>

				<div class="feed" id="feed1">
					<p>W3C </p>
					<div class="heart " id="like1" rel="like"></div>
					<div class="likeCount" id="likeCount1">14</div>
				</div>

				<div class="feed" id="feed2">
					<p>百度一下</p>
					<div class="heart" id="like2" rel="like"></div>
					<div class="likeCount" id="likeCount2">10</div>
				</div>

			</div>

		</article>

		<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

		<script type="text/javascript">
			$(document).ready(function() {

				$('body').on("click", '.heart', function() {

					var A = $(this).attr("id");
					var B = A.split("like");
					var messageID = B[1];
					var C = parseInt($("#likeCount" + messageID).html());
					$(this).css("background-position", "")
					var D = $(this).attr("rel");

					if (D === 'like') {
						$("#likeCount" + messageID).html(C + 1);
						$(this).addClass("heartAnimation").attr("rel", "unlike");
					} else {
						$("#likeCount" + messageID).html(C - 1);
						$(this).removeClass("heartAnimation").attr("rel", "like");
						$(this).css("background-position", "left");
					}
				});

			});
		</script>

	</body>
</html>

2.vue

<template>
	<view class="lock-main">
		<div>
			<div class="heart" :style='{"background-position":bakPo}' :class="{'heartAnimation':heartAnimation}" @click="heartLiske"></div>
			<div class="likeCount">{{likeCount}}</div>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				heartAnimation: false,
				bakPo: 'left',
				likeCount: 14
			}
		},
		methods: {
			heartLiske() {
				this.heartAnimation ? this.likeCount-- : this.likeCount++
				this.bakPo = ""
				this.bakPo = this.heartAnimation ? "left" : "right"
				this.heartAnimation = this.heartAnimation ? false : true
				console.log(this.likeCount)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.lock-main {
		color: #fff;
	}

	.heart {
		background: url('http://product.armark.com.cn/echolac/res/image/Fu3O0aVNZihP-qi7NCH0epcFnju_.png');
		background-repeat: no-repeat;
		height: 100px;
		width: 100px;
		cursor: pointer;
		background-size: 2900%;
	}

	@-webkit-keyframes heartBlast {
		0% {
			background-position: left;
		}

		100% {
			background-position: right;
		}
	}

	@keyframes heartBlast {
		0% {
			background-position: left;
		}

		100% {
			background-position: right;
		}
	}

	.heartAnimation {
		display: inline-block;
		-webkit-animation-name: heartBlast;
		animation-name: heartBlast;
		-webkit-animation-duration: .8s;
		animation-duration: .8s;
		-webkit-animation-iteration-count: 1;
		animation-iteration-count: 1;
		-webkit-animation-timing-function: steps(28);
		animation-timing-function: steps(28);
		background-position: right;
	}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值