首页 技术 正文
技术 2022年11月22日
0 收藏 823 点赞 4,288 浏览 2627 个字

今天我又写了个很酷的实例:星级评分系统(可自定义星星个数、显示信息)
sufuStar.star();使用默认值5个星星,默认信息
var msg = [........]; sufuStar.star(10,msg);自定义星星个数为10、显示信息msg格式参考默认值,条数必须和星星个数一致;

自己实现一些实例,有个好处,能增加应用各知识点的熟练度,还能检验出自己的薄弱项!一经发现,立即翻API文档恶补!

不知道是不是我太笨,这个实例居然写了整整一天!

不废话了,先说下这个实例涉及的知识点:

  1. 用CSS的border来画个三角形,并用before来把它加到其它元素上;
  2. 学习如何用CSS来定位元素;
  3. 学习事件的代理;
  4. 如何优化性能;
  5. String对象的match方法的应用,正则表达式的应用;
  6. 注册事件与事件处理,需要兼容IE的写法;
  7. 学习如何利用‘||’给变量设置默认值;
  8. 简化代码:将可能要重复写的代码拿出来,单独写成一个函数;

下面是带注释的完整代码,碰到不懂得就查文档吧,以我目前的水平只能写成这样了,若有好的建议,欢迎前辈指出!

*{ margin: 0; padding: 0; } #star{ position: absolute; left: 0; right: 0; top: 30px; bottom: 0; margin: auto; width: 80%; font-size: 12px; } #star-div{ margin:5px; font-size: 0; } #star-div a{ display: inline-block; width: 21px; height: 21px; background: url(https://img.zhankr.net/getndufauvx305807.gif) no-repeat; } #star-div .on{ background: url(https://img.zhankr.net/djzvknxrkvh305808.gif) no-repeat; } #star-info{ position: absolute; top: 55px; left: -30px; display: none; width: 155px; height: 50px; padding: 2px; line-height: 17px; border-radius: 8px; background-color: gold; z-index: 5; } #star-info:before{ content: ”; border-bottom: 10px solid gold; border-left: 10px solid rgba(0,0,0,0); border-right: 10px solid rgba(0,0,0,0); position: absolute; left: 35px; top: -10px; } #star-span{line-height: 14px} #star-info strong,#star-span strong{ color: red; } window.onload = function(){ var sufuStar = function (){ //工具函数 function gbyId(id){return document.getElementById(id);} function addEvent(elem,type,func){ //兼容IE if(elem.addEventListener){ elem.addEventListener(type,func,false) }else if(elem.attachEvent){ elem.attachEvent(‘on’+type,func) } } function getIndex(event) { //兼容IE var e = event || window.event; var t = e.target || e.srcElement; if (t.tagName.toLowerCase() === ‘a’) { return parseInt(t.innerHTML); } } function showInfo(index,msg){ var info = gbyId(‘star-info’); info.style.display = ‘block’; info.style.left = index*21-51+’px’; info.innerHTML = “ “+index+’分 ‘+msg[index-1].match(/(.+)\|/)[1]+’
‘+’
‘+msg[index-1].match(/\|(.+)/)[1]; } function appenStar(elem,nums){ var frag = document.createDocumentFragment(); //为了提高性能,因使用DocumentFragment一次性append,这样页面只重新渲染一次 for(var i = 0;i标签的父元素starContainer来代理事件) addEvent(starContainer,’mouseout’,out); addEvent(starContainer,’click’,click); function over(event){ if(getIndex(event)){ //若getIndex(event)取不到值,说明当前触发事件的目标不是标签 var index = getIndex(event); change(index); showInfo(index,msg); } } function out(event){ change(); //将评分设为已点击状态clickStar gbyId(‘star-info’).style.display = ‘none’ } function click(event) { if (getIndex(event)) { var index = getIndex(event); clickStar = index; //保存点击状态 gbyId(‘star-info’).style.display = ‘none’; gbyId(‘star-span’).innerHTML = “” + index + ‘分 ‘ + msg[index – 1].match(/(.+)\|/)[1] + ‘‘+’
‘+ msg[index – 1].match(/\|(.+)/)[1]; } } function change(index){ curentStar = index||clickStar; for(var i=0;i 点击星星打分:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,074
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892