按钮图标

jQuery+css3音量调节旋钮插件

阿里云


这是一款实用 jQuery 和 css3 制作的音量调节旋钮插件。该 jQuery 旋钮的 UI 使用 css3 来渲染,旋钮的旋转控制则使用 jquery 来实现。

HTML 代码:

  1. <figure class="dial">
  2.   <div class="center"><span>0</span></div>
  3.   <div class="wrapper">
  4.     <div class="knob">
  5.       <div class="handle"></div>
  6.       <div class="indicator"></div>
  7.     </div>
  8.   </div>
  9.   <canvas class="progress"></canvas>
  10. </figure>
也想出现在这里?联系我们
创客主机

JAVASCRIPT 代码:

  1. (function() {
  2.   var Dial, dial,
  3.     __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  4.   Dial = (function() {
  5.     Dial.prototype.raf = null;
  6.  
  7.     Dial.prototype.mdown = false;
  8.  
  9.     Dial.prototype.mPos = {
  10.       x: 0,
  11.       y: 0
  12.     };
  13.     Dial.prototype.elementPosition = {
  14.       x: 0,
  15.       y: 0
  16.     };
  17.     Dial.prototype.target = 0;
  18.     Dial.prototype.steps = 60;
  19.     Dial.prototype.radius = 150;
  20.     Dial.prototype.maxDiff = 150;
  21.     Dial.prototype.constraint = 360;
  22.     Dial.prototype.currentVal = 0;
  23.     function Dial($context) {
  24.       var knobOffset;
  25.       this.$context = $context;
  26.       this.onMouseMove = __bind(this.onMouseMove, this);
  27.       this.onMouseUp = __bind(this.onMouseUp, this);
  28.       this.onMouseDown = __bind(this.onMouseDown, this);
  29.       this.$knob = this.$context.find(".knob");
  30.       this.$handle = this.$context.find(".handle");
  31.       this.$progress = this.$context.find(".progress");
  32.       this.$center = this.$context.find(".center");
  33.       this.$textOutput = this.$center.find("span");
  34.       this.ctx = this.$progress.get(0).getContext("2d");
  35.       knobOffset = this.$knob.offset();
  36.       this.elementPosition = {
  37.         x: knobOffset.left,
  38.         y: knobOffset.top
  39.       };
  40.       this.centerX = this.$progress.width() / 2;
  41.       this.centerY = this.$progress.height() / 2;
  42.       this.canvasSize = this.$progress.width();
  43.       this.addEventListeners();
  44.       this.draw();
  45.       return;
  46.     }
  47.     Dial.prototype.addEventListeners = function() {
  48.       this.$context.on("mousedown", this.onMouseDown);
  49.       this.$context.on("mousemove", this.onMouseMove);
  50.       $("body").on("mouseup", this.onMouseUp);
  51.     };
  52.     Dial.prototype.setDialPosition = function() {
  53.       this.$knob.css({
  54.         transform: "rotate(" + this.target + "deg)"
  55.       });
  56.       this.$handle.css({
  57.         transform: "rotate(-" + this.target + "deg)"
  58.       });
  59.       this.draw();
  60.     };
  61.     Dial.prototype.draw = function() {
  62.       var i, _i, _ref;
  63.       this.$progress.get(0).height = this.canvasSize;
  64.       this.$progress.get(0).width = this.canvasSize;
  65.       this.ctx.save();
  66.       this.ctx.translate(this.centerX, this.centerY);
  67.       this.ctx.rotate((-90 * (Math.PI / 180)) - (Math.PI * 2 / this.steps));
  68.       for (i = _i = 0, _ref = this.steps - 1; _i <= _ref; i = _i += 1) {
  69.         this.ctx.beginPath();
  70.         this.ctx.rotate(Math.PI * 2 / this.steps);
  71.         this.ctx.lineWidth = 2;
  72.         this.ctx.lineTo(160, 0);
  73.         this.ctx.lineTo(170, 0);
  74.         if (i <= Math.floor(this.currentVal)) {
  75.           this.ctx.shadowBlur = 10;
  76.           this.ctx.strokeStyle = "#fff";
  77.           this.ctx.shadowColor = "#fff";
  78.           if (i > (this.steps * 0.75) && this.currentVal > (this.steps * 0.75)) {
  79.             this.ctx.strokeStyle = "#ff9306";
  80.             this.ctx.shadowColor = "#ff9306";
  81.           }
  82.           if (i > (this.steps * 0.88) && this.currentVal > (this.steps * 0.88)) {
  83.             this.ctx.strokeStyle = "#ff0606";
  84.             this.ctx.shadowColor = "#ff0606";
  85.           }
  86.         } else {
  87.           this.ctx.strokeStyle = "#444";
  88.           this.ctx.shadowBlur = 0;
  89.           this.ctx.shadowColor = "#fff";
  90.         }
  91.         this.ctx.stroke();
  92.       }
  93.       this.ctx.restore();
  94.     };
  95.     Dial.prototype.setMousePosition = function(event) {
  96.       var atan, diff, target;
  97.       this.mPos = {
  98.         x: event.pageX - this.elementPosition.x,
  99.         y: event.pageY - this.elementPosition.y
  100.       };
  101.       atan = Math.atan2(this.mPos.x - this.radius, this.mPos.y - this.radius);
  102.       target = -atan / (Math.PI / 180) + 180;
  103.       diff = Math.abs(target - this.target);
  104.       if (diff < this.maxDiff && target < this.constraint) {
  105.         this.target = target;
  106.         this.currentVal = this.map(this.target, 0, 360, 0, this.steps);
  107.         this.setDialPosition();
  108.         this.updateOutput();
  109.       }
  110.     };
  111.     Dial.prototype.updateOutput = function() {
  112.       this.$textOutput.text(Math.round(this.currentVal));
  113.     };
  114.     Dial.prototype.onMouseDown = function(event) {
  115.       this.mdown = true;
  116.     };
  117.     Dial.prototype.onMouseUp = function(event) {
  118.       this.mdown = false;
  119.     };
  120.     Dial.prototype.onMouseMove = function(event) {
  121.       if (this.mdown) {
  122.         this.setMousePosition(event);
  123.       }
  124.     };
  125.     Dial.prototype.map = function(value, low1, high1, low2, high2) {
  126.       return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
  127.     };
  128.     return Dial;
  129.   })();
  130.   this.$dial = $(".dial");
  131.   dial = new Dial(this.$dial);
  132. }).call(this);

更多详细信息请参看:http://codepen.io/jon-walstedt/pen/qbjEu

jQuery+css3 音量调节旋钮插件

已有 544 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!