JS原生评分组件

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; 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, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>评分组件</title>
<link rel="stylesheet" href="css/reset.css">
<style>
/*外层底色星星*/
.star-rating{
background-image: url("img/bgimg.svg");
width: 480px;
height: 48px;
background-size: 48px;
cursor: pointer;
position: relative;
}
.star-rating[data-title]:hover::after{
content: attr(data-title);
position: absolute;
left: 0;
background-color: #eee;
padding: 5px 5px;
border-radius: 5px;
}
/*内层星星*/
.star-value{
background-image: url("img/cover.svg");
width: 50%;
height: 100%;
background-size: 48px;
}
</style>
</head>
<body>
<!--外层背景星星和内部覆盖星星-->
<div class="star-rating">
<div class="star-value"></div>
</div>
<div id="rater"></div>
</body>
<script src="js/index.js"></script>
<script>
rater({
element:document.getElementById("rater"),
max:10,
starSize:36,
showTip:true,
step:0.5,
rating:3
});
</script>
</html>
@charset "UTF-8";

body,div,dl,dt,dd,ul,li,pre,form,fieldset,select,input,textarea,button,p,img,iframe{  margin:;  padding:;  }
h1,h2,h3,h4,h5,h6{ font-weight:normal; margin:; padding:; }
body{ width: 100%; font-family: "Arial Narrow","SimSun","宋体"; background-color: #fff; -webkit-overflow-scrolling: touch;overflow-scrolling: touch; }
/* 重置button边框 */
button{ border: none; }
/* 去掉列表前的标识, li 会继承 */
ol,ul{list-style: none;}
/* 让链接默认不显示下划线*/
a{cursor: pointer;text-decoration: none;}
/* 清理浮动 */
.clearfix:before,.clearfix:after{ display: block; content: " "; clear: both; }
/* for ie67*/
.clearfix{zoom:;}
/* HTML5 媒体文件跟 img 保持一致 */
audio,canvas,video{ display: inline-block; *display: inline; *zoom:; }
address,caption,cite,code,dfn,em,th{ font-style: normal; font-weight: normal; }
.box-sizing{ -moz-box-sizing: border-box; -webkit-box-sizing: border-box; -o-box-sizing: border-box; -ms-box-sizing: border-box; box-sizing: border-box; }
/*p{ text-align:justify; text-justify:distribute;}*/
div, p, span { text-overflow: ellipsis; word-break: break-all; word-wrap: break-word; }
/*iphone及ipad下输入框默认内阴影*/
input, button { outline: none; -webkit-appearance: none; }
textarea { resize: none; outline: none; }
img { vertical-align: middle; border:; width: 100%; }
a:active, a:hover { outline:; }
/*ios和android下触摸元素时出现半透明灰色遮罩*/
a, input { border: none; outline: none; -webkit-tap-highlight-color: rgba(255, 255, 255, 0); }
input[type=button] { cursor: pointer; }
input[type=submit] { cursor: pointer; }

reset.css

/*
定义函数 渲染评分组件
*/
function rater(options){
var showTip;
//options为传入的一个参数(为对象)
//判断options是否为一个对象
if(Object.prototype.toString.call(options)!=="[object Object]"){
throw new Error("需要一个字面量对象作为参数");
}
if(typeof options.element === "undefined" || options.element == null){
throw new Error("需要一个dom节点");
}
if(typeof options.showTip === "undefined"){
showTip=true;
}else {
showTip=options.showTip;
}
//console.log(showTip);
if(typeof options.step !== "undefined"){
if(options.step <= 0 || options.step > 1){
throw new Error("step必须是0-1的小数");
}
}
/*
参数:
element:dom 当前作用元素
max:当前最大星星数量
starSize:星星大小
showTip:布尔 true 显示title false 不显示
step:0-1 增长速度
rating:默认几个星星
*/
// 获取参数
var ele=options.element;
var max=options.max||5;
var starSize=options.starSize||48;
var step=options.step||1;
var currentStar;//当前星星数量
var rating;//设置当前记录值 用来判断点击后的记录纸
// Dom操作
ele.style.width=max*starSize+"px";
ele.classList.add("star-rating");
//ele.className += "star-rating";
ele.style.backgroundSize=starSize+"px";
ele.style.height=starSize+"px";
// 创建内部div节点 添加到当前节点中
var createDom=document.createElement("div");
createDom.classList.add("star-value");
createDom.style.backgroundSize=starSize+"px";
// 默认0%
if(!options.rating){
createDom.style.width="0%";
}else{
rating=options.rating;
createDom.style.width=(options.rating/max)*100+"%";
} ele.appendChild(createDom); //mousemove事件函数
function onStarIn(e){
var x=e.offsetX;//父级左边的距离
var eleWidth=ele.offsetWidth;//父级的宽度
var percent=x/eleWidth;//获取百分比
if(step===1){
currentStar=Math.ceil(percent*max);//当前星星数量
}else {
for (var i = step;;i+=step){
if(i>=percent*max){
currentStar=i;
break;
}
}
currentStar=parseFloat(currentStar.toPrecision(12));
}
// 渲染宽度
createDom.style.width=(currentStar/max)*100+"%";
//设置自定义属性
if(showTip){
ele.setAttribute("data-title",currentStar+"/"+max);
}
}
//mouseleave事件函数
function onStarOut(){
if(rating){
createDom.style.width=(rating/max)*100+"%";
}else{
createDom.style.width="0px";
}
}
//click 事件函数
function onStarClick(){
rating=currentStar;
createDom.style.width=(currentStar/max)*100+"%";
}
//绑定事件
ele.addEventListener("mousemove",onStarIn);
ele.addEventListener("mouseleave",onStarOut);
ele.addEventListener("click",onStarClick);
}

运行效果如下:

---星星为背景图片!

---偶尔听到一个课程讲解的,感觉会用到,就留下了。记录于此。

JS原生评分组件的更多相关文章

  1. 原生JS结合cookie实现商品评分组件

    开发思路如下: 1.利用JS直接操作DOM的方式开发商品评分组件,主要实现功能有:显示评价评分的样式以及将用户操作后对应的数据返回到主页面 2.主页面引入商品评分组件的js文件并根据规定格式的数据,生 ...

  2. ExtJS 4.2 评分组件

    上一文章是扩展ExtJS自带的Date组件.在这里将创建一个评分组件. 目录 1. 介绍 2. 示例 3. 资源下载 1. 介绍 代码参考的是 Sencha Touch 2上的一个RatingStar ...

  3. Vue.js学习 Item11 – 组件与组件间的通信

    什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有 ...

  4. Vue.js 的精髓——组件

    开篇:Vue.js 的精髓——组件 写在前面 Vue.js,无疑是当下最火热的前端框架 Almost,而 Vue.js 最精髓的,正是它的组件与组件化.写一个 Vue 工程,也就是在写一个个的组件. ...

  5. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  6. react 实现评分组件

    写了个评分组件,效果如下 组件Rate.js import React, { Component } from 'react' import './Rate.less' export default ...

  7. js原生高逼格插件

    如何定义一个高逼格的原生JS插件 作为一个前端er,如果不会写一个小插件,都不好意思说自己是混前端界的.写还不能依赖jquery之类的工具库,否则装得不够高端.那么,如何才能装起来让自己看起来逼格更高 ...

  8. js原生代码实现轮播图案例

    一.轮播图是现在网站网页上最常见的效果之一,对于轮播图的功能,要求不同,效果也不同! 我们见过很多通过不同的方式,实现这一效果,但是有很多比较麻烦,而且不容易理解,兼容性也不好. 在这里分享一下,用j ...

  9. 《Ext JS模板与组件基本知识框架图----模板》

    最近在整理Ext JS的模板和组件,在参考<Ext JS权威指南>,<Ext JS Web应用程序开发指南>,<Ext JS API>等相关书籍后才写下这篇< ...

随机推荐

  1. OpenCV 小图重叠至大图指定位置

    Android OpenCV Java: Codes: smallImg.copyTo( bigImg.submat( y, smallImg.rows(), x, smallImg.cols() ) ...

  2. 【2014-08-23】Beyong Coding

        be a product engineer,not be a engineer 选择比努力更重要   just code it,code it ,until you make it 缺的不是i ...

  3. VSTO 开发中 应用ActionPane、CustomTaskPane

    以Excel插件为例: 1. ActionPane 创建 ThisWorkbook 项目 private void ThisWorkbook_Startup(object sender, System ...

  4. ping -l 1000 -t 与ping -t的区别

    windows -l 1000的意思是规定发送的包的大小是1000字节如果不加这个参数的话,就发送包默认为32字节还有-t就是一直发送,直到手动停止

  5. July 03rd 2017 Week 27th Monday

    Even if you are on the right track, you will get run over if you just sit there. 即使你处于正确的轨道上,但如果你只是坐 ...

  6. 财务软件(gnucash)

    apt-get install gnucash

  7. Yii日志使用

    Yii 提供了一个灵活可扩展的日志功能.记录的日志 可以通过日志级别和信息分类进行归类.通过使用 级别和分类过滤器,所选的信息还可以进一步路由到 不同的目的地,例如一个文件,Email,浏览器窗口等. ...

  8. PowerDNS简单教程(1):安装篇

    这一篇开始直接是PowerDNS教程,连续四篇.DNS的相关背景知识我就不介绍了,有需要的话看看 http://baike.baidu.com/link?url=QcthFpAb2QydMqcMJr9 ...

  9. org.openqa.selenium.WebDriverException: unknown error: missing or invalid 'entry.level'

    错误原因:chrome与chromedriver版本号不匹配,升级后即可解决

  10. Yii 不完全解决方案(一)

    此文意在记录 Yii 开发过程中的小问题解决方案 1. Yii 中 Js 和 Css 文件的引入. 我们就从最简单的问题开始吧,说起来也不是问题,只是语法罢了.假设我们的 js 文件都放在和 prot ...