搜索
首页web前端html教程纯CSS实现星型⭐️评级_html/css_WEB-ITnose

今天,我们用纯CSS实现⭐️评级。

案例效果

分析:正常情况下为空心的☆,鼠标hover时,变为实心黄色的★.

HTML分析

div.star包裹5个span,每个span的内容为空心的☆.HTML代码如下:

<div class="star">    <span>☆</span><span>☆</span><span>☆</span><span>☆</span><span>☆</span>  </div>
CSS分析

1.分析:当我们hover时,我们用实心的★覆盖空心的☆,并给实心的★设置color:gold;.

要覆盖空心的☆,我们要利用为元素::before,并赋予content:'★'.同时为了实现覆盖,而不是在之前添加内容,我们需要为元素设置position:absolute;.

这时,当你hover时,hover的☆就变为实体黄色的⭐️了。

2.接下来,我们要实现当我hover某个☆时,不仅这一个,它前面的☆也都要变为黄色实体的⭐️。
首先,我们要用到通用兄弟选择器~

在使用 ~ 连接两个元素时,它会匹配第二个元素,条件是它必须跟(不一定是紧跟)在第一个元素之后,且他们都有一个共同的父元素 .
比如:div~p就会匹配与div同辈的且在div之后的所有p元素。

当我们hover某个☆时,我们利用通用兄弟选择器使后面的☆也同时变为黄色实体星⭐️。

span:hover::before,span:hover~span::before{  content:'★';  color:gold;  position:absolute;} 

这时,当你hover某个☆时,这个及其后面的☆都同时变为黄色实体星⭐️。

3.最后,我们利用unicode-bidi,direction属性,使文本由右向左显示。

The unicode-bidi CSS property together with the direction property relates to the handling of bidirectional text in a document.For example, if a block of text contains both left-to-right and right-to-left text then the user-agent uses a complex Unicode algorithm to decide how to display the text. This property overrides this algorithm and allows the developer to control the text embedding.

这样的话,当我们hover时,还是这个及其后面的☆都同时变为黄色实体星⭐️,但是由于此时从右向左显示,它的后面兄弟元素就变到“前面”来了。

.star{unicode-bidi: bidi-override;direction:rtl;}

对内联元素应用direction时要注意:

For the direction property to have any effect on inline-level elements, the unicode-bidi property's value must be embed or override.

OK.用纯CSS实现⭐️评级的效果就实现了!

CSS代码如下:

span:hover::before,span:hover~span::before{  content:'★';  color:gold;  position:absolute;} .star{  unicode-bidi:bidi-overrride;  direction:rtl;}
用js实现评级效果

思路:当元素触发onmouseover事件时,赋予不同的class值。

代码如下:

 <div class="heart">    <a href="" id="one" class="heart-off"></a>    <a href="" id="two" class="heart-off"></a>    <a href="" id="three" class="heart-off"></a>    <a href="" id="four" class="heart-off"></a>    <a href="" id="five" class="heart-off"></a>  </div>
.heart-off,.heart-on,.heart-hover{  text-decoration:none;}.heart-off:before,.heart-on:before,.heart-hover:before{  content:'\2665';}.heart-off{color:rgba(150,150,150,.5);}.heart-on{color:rgba(255,0,0,.5);}.heart-hover{color:rgba(255,0,0,1);}
one.onmouseover=function(){  this.className="heart-hover";  two.className="heart-off";  three.className="heart-off";  four.className="heart-off";  five.className="heart-off";};two.onmouseover=function(){  this.className="heart-hover";  one.className="heart-on";  three.className="heart-off";  four.className="heart-off";  five.className="heart-off";};three.onmouseover=function(){  this.className="heart-hover";  one.className="heart-on";  two.className="heart-on";  four.className="heart-off";  five.className="heart-off";};four.onmouseover=function(){  this.className="heart-hover";  one.className="heart-on";  two.className="heart-on";  three.className="heart-on";  five.className="heart-off";};five.onmouseover=function(){  this.className="heart-hover";  one.className="heart-on";  two.className="heart-on";  three.className="heart-on";  four.className="heart-on";};
参考资料

1.字符实体
2.字符集
3.[unicode-bidide的用法](https://developer.mozilla.org/en-US/docs/Web/CSS/unicode-bidi)

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
HTML标签和HTML属性有什么区别?HTML标签和HTML属性有什么区别?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

HTML的未来:进化和趋势HTML的未来:进化和趋势May 13, 2025 am 12:01 AM

HTML的未来将朝着更加语义化、功能化和模块化的方向发展。1)语义化将使标签更明确地描述内容,提升SEO和无障碍访问。2)功能化将引入新元素和属性,满足用户需求。3)模块化将支持组件化开发,提高代码复用性。

为什么HTML属性对Web开发很重要?为什么HTML属性对Web开发很重要?May 12, 2025 am 12:01 AM

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外观和功能

Alt属性的目的是什么?为什么重要?Alt属性的目的是什么?为什么重要?May 11, 2025 am 12:01 AM

alt属性是HTML中标签的重要部分,用于提供图片的替代文本。1.当图片无法加载时,alt属性中的文本会显示,提升用户体验。2.屏幕阅读器使用alt属性帮助视障用户理解图片内容。3.搜索引擎索引alt属性中的文本,提高网页的SEO排名。

HTML,CSS和JavaScript:示例和实际应用HTML,CSS和JavaScript:示例和实际应用May 09, 2025 am 12:01 AM

HTML、CSS和JavaScript在网页开发中的作用分别是:1.HTML用于构建网页结构;2.CSS用于美化网页外观;3.JavaScript用于实现动态交互。通过标签、样式和脚本,这三者共同构筑了现代网页的核心功能。

如何在标签上设置lang属性?为什么这很重要?如何在标签上设置lang属性?为什么这很重要?May 08, 2025 am 12:03 AM

设置标签的lang属性是优化网页可访问性和SEO的关键步骤。1)在标签中设置lang属性,如。2)在多语言内容中,为不同语言部分设置lang属性,如。3)使用符合ISO639-1标准的语言代码,如"en"、"fr"、"zh"等。正确设置lang属性可以提高网页的可访问性和搜索引擎排名。

HTML属性的目的是什么?HTML属性的目的是什么?May 07, 2025 am 12:01 AM

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外观和互动,使网站互动,响应式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,类,类型,类型,和dissabledtransfransformformformformformformformformformformformformformformforment

您如何在HTML中创建列表?您如何在HTML中创建列表?May 06, 2025 am 12:01 AM

toCreateAlistinHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulleTedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,customeizableWithTheTtheTthetTheTeTeptTributeFordTributeForderForderForderFerentNumberingSnumberingStyls。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具