在打开文章时,判断用户是否点赞或者收藏,切换显示点赞收藏图标;

在做到某项目的文章显示时,考虑到用户的操作体验,添加了用户的点赞,评论,收藏行为;

首先去阿里图标矢量库;找几个关于点赞收藏评论的图标;
下载下来;

找到的这几张图标还不错;在这里插入图片描述

大概写个样式,试试效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<div style="width:660px;">
			 <!-- 点赞图标存放处-->
			<div style="width:220px; float: left;">
				<a href="javaScript:void(0)" onclick="toLink()">
				<img style=" width: 74px; height: 74px; margin-right: 146px;"  src="img/dzPre.png"/>
				</a>
			</div>
			<!-- 评论图标存放处 -->
			<div style="width:220px; float: left;">
				<a href="javaScript:void(0)" onclick="toComment()">
				<img style=" width: 74px; height: 74px;margin-left: 73px;margin-right: 73px;"  src="img/plPre.png"/>
				</a>
			</div>
			<!-- 收藏图标存放处 -->
			<div style="width:220px; float: left;">
				<a href="javaScript:void(0)" onclick="toCollect()">
				<img style=" width: 74px; height: 74px;margin-left: 146px;"  src="img/scPre.png"/>
				</a>
			</div>
			<!-- 清除浮动 -->
			<div style="clear: left;"></div>	
		</div>
		<!-- 预先留给评论区的区域 -->
		<div style="width:660px;" id="commentArea"></div>
	</body>
</html>

效果;
在这里插入图片描述

然后考虑到该交流平台的实际使用; 若用户没有登录;就直接放置三个白色的点赞,评论,收藏图片;
(使用session进行判断)
在这里插入图片描述
然后还需要考虑到该用户若是登录了,就去根据用户的Id和当前文章的Id查询出点赞与收藏关系;
用Map集合将两个结果存储起来;
分别用不同的键"like""collect";
然后根据查询到的信息,决定是否为点赞或者收藏了;
最后将取得值存入隐藏的input标签中;

//这里根据用户是否登录,决定是否去查询当前用户是否点赞与收藏了;
            if (userAcc != null) {
                //若登录了,就去查询一下信息;把并且存到下面的隐藏域中,这样的话,后面判断是否点赞/收藏就比较方便了;
                $.get("../../showNews/isLikeAndCollectByNewIdAndUserId", {
                    userId: userId,
                    newId: newId
                }, function (res) {
                    if (res.code == 500) {
                        alert("哎呦,出错了");
                    } else if (res.code == 200) {
                        //把得到的数据放到隐藏域;
                        $("input[name='isLike']").val(res.data.like);
                        $("input[name='isCollect']").val(res.data.collect);
                    }
                })
            }

在这里插入图片描述

控制层处理

/**
* @author by 小智RE0
 * 用户登录状态下,根据用户的Id和新闻的Id查询一下是否存在着点赞或者收藏;
 * @param userId    用户的Id
 * @param newId     新闻的Id
 */
@ResponseBody
@GetMapping(value = "/isLikeAndCollectByNewIdAndUserId")
public CommonResult isLikeAndCollectByNewIdAndUserId(Integer userId,Integer newId){
    CommonResult commonResult = null;
    try{
        Map<String,String> isLikeOrCollect =showNewsService.isLikeAndCollectByNewIdAndUserId(userId,newId);
        commonResult  = new CommonResult(200,"显示新闻时;根据用户Id和新闻Id查询点赞与收藏",isLikeOrCollect);
    }catch (Exception e){
        e.printStackTrace();
        commonResult = new CommonResult(500,"显示新闻时;根据用户Id和新闻Id查询点赞与收藏出现异常",null);
    }
    return commonResult;
}

服务层处理

/**
* @author by 小智RE0
* 用户登录状态下,根据用户的Id和新闻的Id查询一下是否存在着点赞或者收藏;
* @param userId 用户的Id
* @param newId  新闻的Id
*/
public Map<String, String> isLikeAndCollectByNewIdAndUserId(Integer userId, Integer newId) {
   //先查询点赞关系;查询到1的话就说明点赞了
   Integer like =  showNewsDao.isLikeByNewIdAndUserId(userId,newId);
   //在查询收藏关系;查询到1的话就说明收藏了;
   Integer collect = showNewsDao.isCollectByNewIdAndUserId(userId,newId);

   //测试查看;
   System.out.println("-----------当前用户点赞了-------"+like);
   System.out.println("-----------当前用户收藏了-------"+collect);
   Map<String, String> map = new HashMap<>();
   if(like.equals(1)){
       map.put("like","alreadyLike");
   }else{
       map.put("like","noLike");
   }

   if(collect.equals(1)){
       map.put("collect","alreadyCollect");
   }else{
       map.put("collect","noCollect");
   }

   return map;
}

数据交互层处理

/**
* 查询新闻与用户的点赞关系,得到结果;0没有点赞/1点赞了
* @param userId 用户Id
* @param newId  新闻Id
*/
Integer isLikeByNewIdAndUserId(@Param("userId") Integer userId, @Param("newId") Integer newId);

/**
* 查询新闻与用户的收藏关系,得到结果;0没有收藏/1收藏了
* @param userId 用户Id
* @param newId  新闻Id
*/
Integer isCollectByNewIdAndUserId(@Param("userId") Integer userId, @Param("newId") Integer newId);

SQL语句

<!--查询新闻与用户的点赞关系: 0:没有点赞  1:点赞了-->
<select id="isLikeByNewIdAndUserId" resultType="integer">
    SELECT count(1) FROM user_like_news WHERE user_id = #{userId} AND news_id = #{newId}
</select>

<!--查询新闻与用户的收藏关系: 0:没有收藏  1:收藏了-->
<select id="isCollectByNewIdAndUserId" resultType="integer">
    select count(1) from user_collect where user_id = #{userId} and  news_id = #{newId}
</select>

数据表

在这里插入图片描述

在文章查询显示的过程中;根据情况判断,进行拼接图片;
在这里插入图片描述

收藏且点赞 ;在这里插入图片描述

点赞,未收藏;在这里插入图片描述

未点赞,未收藏;在这里插入图片描述

### 创建图标组件并实现击交互 为了实现在 Vue 项目中的图标显示及其交互效果,可以按照如下方法构建一个可重用的按钮组件。 #### 定义按钮组件 `LikeButton` 首先定义一个新的单文件组件`like-button.vue`: ```html <template> <button @click="toggleLike" :class="{ liked: isLiked }"> <span v-if="isLiked">❤️</span> <!-- 已状态 --> <span v-else>? </span> <!-- 未状态,默认问号作为占位符 --> </button> </template> <script> export default { name: 'LikeButton', data() { return { isLiked: false, // 初始状态下设为未 }; }, methods: { toggleLike() { this.isLiked = !this.isLiked; } } }; </script> <style scoped> button { background-color: transparent; border: none; cursor: pointer; } .liked { color: red; /* 当前已改变颜色 */ } </style> ``` 此代码片段展示了如何创建一个简单的喜欢/取消喜欢按钮[^1]。当用户击该按钮,会触发`toggleLike()`函数切换当前的状态,并相应更新视图上的图标样式。 #### 注册全局组件以便在整个应用中使用 为了让这个自定义组件能够在整个应用程序的不同部分被轻松调用而不必每次都重新导入它,在主入口文件(通常是`main.js`)里注册成全局组件是一个不错的选择[^2]。 ```javascript import LikeButton from './components/LikeButton'; // 将其设置为全局可用的Vue组件 Vue.component('like-button', LikeButton); new Vue({ render: h => h(App), }).$mount('#app'); ``` 这样一来,在项目的任意位置都可以直接通过标签名 `<like-button></like-button>` 来插入和使用这个按钮了。 #### 动画增强用户体验 对于更高级的需求来说,还可以考虑加入一些动画效果来提升用户的体验感。例如利用CSS3的关键帧动画模拟气泡升起的效果或者简单的变化过渡等[^5]。 ```css @keyframes heartBeat { 0% { transform: scale(1); opacity: .7;} 50% {transform: scale(.8);} 100% {transform: scale(1);opacity:.9;} } button span{ display:inline-block; transition: all ease-in-out 0.3s; } button.liked span { animation-name:heartBeat ; animation-duration: 0.6s; animation-fill-mode:forwards; } ``` 上述 CSS 添加了一个名为 `heartBeat` 的关键帧动画序列给已经的心形符号加上跳动的感觉,使得操作反馈更加生动有趣。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小智RE0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值