这是一个原生JS、CSS实现的转盘效果(题目在这:http://www.cnblogs.com/arfeizhang/p/turntable.html),花了半个小时左右,准备睡觉,所以先贴一段代码,预计会抽空优化,让它在手机上也能运行;另外,如果看代码的时候有什么问题,请留言。。。

 <!DOCTYPE html>
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>转盘</title>
<style>
.holder {
width: 550px;
display: block;
margin: 10px auto;
padding: 20px;
border: 3px solid black;
border-radius: 10px;
position: relative;
}
.rotate-pointer {
display: block;
position: absolute;
width: 521px;
line-height: 10px;
top: 270.5px;
left: 37px;
-webkit-transition:all 4s ease-in-out;
}
#rules {
width: 260.5px;
height: 10px;
display: inline-block;
background-color: black;
}
</style>
</head> <body>
<div class="holder">
<img src="https://images0.cnblogs.com/i/636015/201406/151737329993303.jpg" alt="">
<div id="pointer" class="rotate-pointer">
<div id="rules"></div>
</div>
</div>
<script type="text/javascript">
window.onload = function() {
var table = document.getElementsByClassName('holder')[0],
tablePointer = document.getElementById('pointer'),
getRandom = function(min, max) {
max = max || 1000;
min = min || 0;
return Math.floor(Math.random() * (max - min) + min);
},
degree = 0,
min_circle_times = 2,
max_circle_times = 6,
translate = function() {
degree += getRandom(min_circle_times * 360, max_circle_times * 360); requestAnimationFrame(function() {
tablePointer.style.webkitTransform = 'rotate(' + degree + 'deg)';
});
};
table.onclick = translate; };
</script>
</body> </html>

  


  昨天放出上述内容后,收到题目博主的评论,发现好像有些地方不符合需求,恩,又再试了一个版本,这个版本目前是支持手机的,不过还是只能在webkit内核下使用哦,源码如下:

<!DOCTYPE html>
<html> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>转盘</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
body {
margin:0px;
}
.holder {
text-align: center;
overflow: hidden;
display: block;
margin: 10px auto;
border: 3px solid black;
border-radius: 10px;
position: relative;
}
#table {
cursor: pointer;
display: inline-block;
max-width: 521px;
width: 100%;
-webkit-transition:all 4s ease-in-out;
}
</style>
</head> <body>
<div class="holder">
<img id="table" src="https://images0.cnblogs.com/i/631009/201406/181334125825974.png" alt="">
</div>
<script type="text/javascript">
window.onload = function() {
var degreeCount = 0,
table = document.getElementById('table'),
table_rect = table.getBoundingClientRect(),
circle_center = {
x: table_rect.width / 2 + table_rect.left,
y: table_rect.height / 2 + table_rect.top
},
min_circle_times = 2,
max_circle_times = 6,
getRandom = function(min, max) {
max = max || 1000;
min = min || 0;
return Math.floor(Math.random() * (max - min) + min);
}, // 根据两点求角度
getDegreeByPoint = function(start, enb) {
var tan1 = (start.y - circle_center.y) / (start.x - circle_center.x),
degree1 = Math.round(360 * Math.atan(tan1) / Math.PI),
tan2 = (enb.y - circle_center.y) / (enb.x - circle_center.x),
degree2 = Math.round(360 * Math.atan(tan2) / Math.PI);
return -(degree1 - degree2);
},
rotate_rings = 0,
rotate = function(degree) {
degree = degree - 0;
if (Number.isNaN(degree)) {
degree = degreeCount += getRandom(min_circle_times * 360, max_circle_times * 360);
} else {
degree += degreeCount;
}
requestAnimationFrame(function() {
table.style.webkitTransform = 'rotate(' + degree + 'deg)';
});
}; // 事件监听器
var InputListener = function(tableId) {
this.tableId = tableId;
this.events = {}; if (window.navigator.msPointerEnabled) {
//Internet Explorer 10 style
this.eventTouchstart = "MSPointerDown";
this.eventTouchmove = "MSPointerMove";
this.eventTouchend = "MSPointerUp";
} else {
this.eventTouchstart = "touchstart";
this.eventTouchmove = "touchmove";
this.eventTouchend = "touchend";
} this._beginListen();
};
InputListener.prototype = {
on: function(event, callback) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(callback);
},
emit: function(event, data) {
var callbacks = this.events[event];
if (callbacks) {
callbacks.forEach(function(callback) {
callback(data);
});
}
},
_start: function(event) {
this.emit("start", event);
},
_rotate: function(event) {
this.emit("rotate", event);
},
_stop: function(event) {
this.emit("stop", event);
},
_celebrate: function(event) {
this.emit("celebrate", event);
},
_click: function(event) {
this.emit("click", event);
},
_getPointByEvent: function(event, toucherName) {
toucherName = toucherName || 'touches';
var point = null;
if (window.navigator.msPointerEnabled) {
point = {
x: event.pageX,
y: event.pageY,
time: Date.now()
};
} else {
point = {
x: event[toucherName][0].clientX,
y: event[toucherName][0].clientY,
time: Date.now()
};
}
return point;
},
_beginListen: function() {
var self = this,
table = document.getElementById(this.tableId),
startPoint = null,
movePath = []; // Respond to direction keys
table.addEventListener('click', function(event) {
self._click();
}); table.addEventListener(this.eventTouchstart, function(event) {
if ((!window.navigator.msPointerEnabled && event.touches.length > 1) ||
event.targetTouches > 1) {
return; // Ignore if touching with more than 1 finger
}
startPoint = self._getPointByEvent(event); movePath = [startPoint];
self._start({
start: startPoint
}); event.preventDefault();
}); table.addEventListener(this.eventTouchmove, function(event) {
var endpoint = self._getPointByEvent(event);
if ( !! endpoint) {
movePath.push(endpoint);
self._rotate({
degree: getDegreeByPoint(startPoint, endpoint),
start: startPoint,
end: endpoint
});
}
event.preventDefault();
}); table.addEventListener(this.eventTouchend, function(event) {
if ((!window.navigator.msPointerEnabled && event.touches.length > 0) ||
event.targetTouches > 0) {
return; // Ignore if still touching with one or more fingers
}
var endpoint = self._getPointByEvent(event, 'changedTouches'),
countToCal = 3,
len = movePath.length; self._stop({
degree: getDegreeByPoint(startPoint, endpoint),
start: startPoint,
end: endpoint
});
movePath.push(endpoint);
if (len <= 3) {
self._click({
start: startPoint
});
} else {
var p1 = movePath[len - 1],
p2 = movePath[len - 1 - 3],
// 转动的弧度
degree = getDegreeByPoint(p2, p1),
time = p1.time - p2.time,
speed = degree / time * 1000,
// 速度转换为期望的周数,4指的是CSS动画时间
expectDegree = (speed * 4) + min_circle_times;
self._celebrate({
start: p2,
end: p1,
degree: expectDegree
});
}
event.preventDefault();
});
}
}; var listener = new InputListener('table');
listener.on('start', function(e) {
table.style.webkitTransition = 'all 0s';
}); listener.on('rotate', function(e) {
console.log(e.degree);
rotate(e.degree);
}); listener.on('stop', function(e) {
degreeCount += e.degree;
//degreeCount = degreeCount % 360;
table.style.webkitTransition = 'all 4s ease-in-out';
}); listener.on('celebrate', function(e) {
rotate(e.degree);
}); listener.on('click', rotate);
};
</script>
</body> </html>

一个可以在线运行的地址:http://www.w3cfuns.com/home.php?mod=space&uid=5446932&do=blog&quickforward=1&id=5398670

原生JS、CSS实现的转盘效果(目前仅支持webkit)的更多相关文章

  1. 使用原生JS+CSS或HTML5实现简单的进度条和滑动条效果(精问)

    使用原生JS+CSS或HTML5实现简单的进度条和滑动条效果(精问) 一.总结 一句话总结:进度条动画效果用animation,自动效果用setIntelval 二.使用原生JS+CSS或HTML5实 ...

  2. 实用js+css多级树形展开效果导航菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. js+css实现带缓冲效果右键弹出菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 原生js实现canvas气泡冒泡效果

    说明: 本文章主要分为ES5和ES6两个版本 ES5版本是早期版本,后面用ES6重写优化的,建议使用ES6版本. 1, 原生js实现canvas气泡冒泡效果的插件,api丰富,使用简单2, 只需引入J ...

  5. 使用原生js 实现点击消失效果

    JQ版 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...

  6. 利用tween,使用原生js实现模块回弹动画效果

    最近有一个需求,就是当屏幕往下一定像素时,下方会有一个隐藏的模块马上显现出来,向上运动后带有回弹效果.然后屏幕滚回去时这个模块能够原路返回 其实这个效果css3就可以很轻松实现,但是公司要求最低兼容i ...

  7. 原生js+css实现重力模拟弹跳系统的登录页面

    今天小颖把之前保存的js特效视频看了一遍,跟着视频敲了敲嘻嘻,用原生js实现一个炫酷的登录页面.怎么个炫酷法呢,看看下面的图片大家就知道啦. 效果图: 不过在看代码之前呢,大家先和小颖看看css中的o ...

  8. 原生JS实现幻灯片轮播效果

    在以往的认知中,一直以为用原生JS写轮播是件很难得事情,今天上班仿照网上的写了一个小demo.小试牛刀. 大致效果: html结构很简单,两个列表,一个代表图片列表,一个是右下角序号列表. <d ...

  9. 原生js简单实现拖拽效果

    实现弹窗拖拽效果的原理是:按下鼠标并移动——拖拽移动物体,抬起鼠标——停止移动.主要触发三个事件:onmousedown.onmousemove以及onmouseup: 首先搭建结构:一个宽350px ...

随机推荐

  1. GDI学习之俄罗斯方块续

    当前方块对象 #region 定义砖块int[i,j,y,x] Tricks:i为那块砖,j为状态,y为列,x为行 private int[, , ,] Tricks = {{ { {,,,}, {, ...

  2. oracle定时任务

    一.简介 当我们需要oracle数据库定时自动执行一些脚本,或进行数据库备份.数据库的性能优化,包括重建索引等工作是需要使用到定时任务. 定时任务可以使用以下两种完成. 1.操作系统级的定时任务,wi ...

  3. 一对一关联查询时使用relation连贯操作查询后,调用getLastSql()方法输出的sql语句

    如题: 一对一关联查询时使用relation连贯操作查询后,调用getLastSql()方法输出的sql语句不是一条关联查询语句. 例如: $list = $db->relation(true) ...

  4. centos7 + php7 lamp全套最新版本配置,还有mongodb和redis

    我是个懒人,能yum就yum啦 所有软件的版本一直会升级,注意自己当时的版本是不是已经更新了. 首先装centos7 如果你忘了设置swap分区,下面的文章可以教你怎么补一个上去: http://ww ...

  5. [算法] Manacher算法线性复杂度内求解最长回文子串

    参考:http://www.felix021.com/blog/read.php?2040 以上参考的原文写得很好,解析的非常清楚.以下用我自己的理解,对关键部分算法进行简单的描述: 回文的判断需要完 ...

  6. Linux搭建DNS服务器

    Linux系统信息: Version: Centos 6.6 Ip address:10.0.0.104 Hostname: extmail.com 配置系统 hostname Vim /etc/sy ...

  7. struts2 基本用法

    Struts2必需库: commons-fileupload.jar.commons-io-1.3.2.jar.freemarker-2.3.16.jar.javassist-3.7.ga.jar.o ...

  8. linux网络相关配置文件

    linux系统一般来说分为两大类:1.RedHat系列:Redhat.Centos.Fedora等:2.Debian系列:Debian.Ubuntu等. linux系统中,TCP/IP网络是通过若干个 ...

  9. LeetCode题解-----Majority Element II 摩尔投票法

    题目描述: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The a ...

  10. 【ASP.NET 基础】Page类和回调技术

    Page 类有一个 IsPostBack 属性,这个属性用来指示当前页面是第一次加载还是响应了页面上某个控件的服务器事件导致回发而加载. 1.asp.net页面的声明周期 asp.net页面运行的时候 ...