css实现评分效果,其实是css sprit (css精灵)的延伸应用,效果的实现主要是由 background-position 属性移动图片位置。之前看到有前辈写过关于这方面的内容,在理解上稍有偏差。
我的理解:有人认为background-position 的位置移动中,这个属性相当于大图片不动,把div的顶点进行移动,移动到目标小图的顶点位置,
可参见:http://www.cnblogs.com/iyangyuan/archive/2013/06/01/3111704.html ,我个人认为应该是div位置不变,变得是图片的位置,通过background-position将所需要的小图标定位到div的 0 ,0 位置从而完成小图标显示。并且,移动中 background-position 的值是要变为负值的,说明图片是向上移动的。
单图标定位 在上边的博客写的很详细,所以直接写 五星评分效果实现:
为了更好弄明白,把图加工了一下:
第一步:
养成良好编程习惯将每部分功能放进一个独立部分进行封装,所以在此直接 设置一个div元素:
<div class="rating"></div>
第二步:
由于五星评分需要五个不带边框的方块,很自然要想将五个方块统一起来使用 ul 和 li标签最好,由于实现点击效果,承载元素为 a 标签:
<div class="rating"> <ul> <li class="one"> <a href="#">1</a></li> <li class="two"> <a href="#">2</a></li> <li class="three"> <a href="#">3</a></li> <li class="four"> <a href="#">4</a></li> <li class="five"> <a href="#">5</a></li> </ul></div>
第三步:
结构搭好后开始进行 css 的设计:
ul.rating { position: relative; list-style: none; background: url("../images/star-matrix.gif") no-repeat 0 0; width: 80px; height: 16px;}ul.rating li { float: left; text-indent: -9999px; cursor: pointer;}ul.rating li a { text-decoration: none; width: 16px; height: 16px; position: absolute; top: 0; left: 0;<strong> z-index: 10;/* 与 hover 中的z-index 对比实现另一重要效果,见下边说明 初步设计时可不考虑*/</strong>}/*仅仅是将 A 标签的区域定位至该出现的 UL 区域之中*/ul.rating li.one a {top: 0; left: 0;}ul.rating li.two a {top:0px; left: 16px;}ul.rating li.three a {top:0px; left: 32px;}ul.rating li.four a {top:0px; left: 48px;}ul.rating li.five a {top: 0px; left: 64px;}
第四步:通过以上代码基本上完成了初步效果,接下来是鼠标划过小星星时的效果:
思想是,在鼠标放在当前 A 标签时,A 标签的区域 变得 和 UL 区域一样大,通过background-position 属性定位到所对应的效果图片,并将其盖在之前空白星星上。ul.rating li a:hover{ width: 80px; height: 16px; overflow: hidden; left: 0; top: 0; background: url("../images/star-matrix.gif") no-repeat 0 0; z-index: 2; /* 要实现 遮盖之前背景图像 功能使用 z-index 使当前对应图像图层浮于原图层之上 */}ul.rating li.one a:hover{ background-position: 0 -96px;}ul.rating li.two a:hover{ background-position: 0 -112px;}ul.rating li.three a:hover{ background-position: 0 -128px;}ul.rating li.four a:hover{ background-position: 0 -144px;}ul.rating li.five a:hover{ background-position: 0 -160px;}
第五步:至此已完成五分好评的效果(第三步没用 z-index 属性 ,加粗部分),但是当鼠标滑过时发现只能选择一次得分,不能向后或向前,原因是 LI 中的所有 A 都被盖在最下层,所以在第三步用 z-index 属性 ,使 A 标签时刻处以最上层,才能响应 鼠标指针浮动在元素上出现相应的效果。

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
