按钮图标

knobKnob - 旋钮控制按钮

阿里云


knobKnob 是一款实用 jQuery 和 css3 制作的旋钮控制按钮,knobKnob 这款 jQuery 和 css3 旋钮效果非常逼真,可用鼠标控制旋钮刻度的变化。

HTML 代码:

html 结构非常简单,只需要放一个空的 id 为 control 的 div 即可,后面的 html 代码由 jQuery 在运行时动态产生。

也想出现在这里?联系我们
创客主机
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <meta charset="utf-8" />
  5.         <title>Shiny Switches with CSS3 & jQuery | Tutorialzine Demo</title>
  6.  
  7.         <!-- CSS stylesheets -->
  8.         <link rel="stylesheet" href="assets/css/styles.css" />
  9.         <link rel="stylesheet" href="assets/knobKnob/knobKnob.css" />
  10.  
  11.         <!--[if lt IE 9]>
  12.           <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  13.         <![endif]-->
  14.     </head>
  15.  
  16.     <body>
  17.  
  18.     <section id="main">
  19.  
  20.       <div id="bars">
  21.         <div id="control">
  22.           <!-- The knob markup will go here -->
  23.         </div>
  24.                                 <!-- The colorful dividers will go here -->
  25.       </div>
  26.  
  27.     </section>
  28.  
  29.         <!-- JavaScript includes -->
  30.     <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
  31.     <script src="assets/knobKnob/transform.js"></script>
  32.     <script src="assets/knobKnob/knobKnob.jquery.js"></script>
  33.     <script src="assets/js/script.js"></script>
  34.  
  35.     </body>
  36. </html>

JAVASCRIPT 代码:

  1. (function($){
  2.  
  3.   $.fn.knobKnob = function(props){
  4.  
  5.     var options = $.extend({
  6.       snap: 0,
  7.       value: 0,
  8.       turn: function(){}
  9.     }, props || {});
  10.  
  11.     var tpl = '<div class="knob">\
  12.         <div class="top"></div>\
  13.         <div class="base"></div>\
  14.       </div>';
  15.  
  16.     return this.each(function(){
  17.  
  18.       var el = $(this);
  19.       el.append(tpl);
  20.  
  21.       var knob = $('.knob',el)
  22.         knobTop = knob.find('.top'),
  23.         startDeg = -1,
  24.         currentDeg = 0,
  25.         rotation = 0,
  26.         lastDeg = 0,
  27.         doc = $(document);
  28.  
  29.       if(options.value > 0 && options.value <= 359){
  30.         rotation = currentDeg = options.value;
  31.         knobTop.css('transform','rotate('+(currentDeg)+'deg)');
  32.         options.turn(currentDeg/359);
  33.       }
  34.  
  35.       knob.on('mousedown', function(e){
  36.  
  37.         e.preventDefault();
  38.  
  39.         var offset = knob.offset();
  40.         var center = {
  41.           y : offset.top + knob.height()/2,
  42.           x: offset.left + knob.width()/2
  43.         };
  44.  
  45.         var a, b, deg, tmp,
  46.           rad2deg = 180/Math.PI;
  47.  
  48.         knob.on('mousemove.rem',function(e){
  49.  
  50.           a = center.y - e.pageY;
  51.           b = center.x - e.pageX;
  52.           deg = Math.atan2(a,b)*rad2deg;
  53.  
  54.           // we have to make sure that negative
  55.           // angles are turned into positive:
  56.           if(deg<0){
  57.             deg = 360 + deg;
  58.           }
  59.  
  60.           // Save the starting position of the drag
  61.           if(startDeg == -1){
  62.             startDeg = deg;
  63.           }
  64.  
  65.           // Calculating the current rotation
  66.           tmp = Math.floor((deg-startDeg) + rotation);
  67.  
  68.           // Making sure the current rotation
  69.           // stays between 0 and 359
  70.           if(tmp < 0){
  71.             tmp = 360 + tmp;
  72.           }
  73.           else if(tmp > 359){
  74.             tmp = tmp % 360;
  75.           }
  76.  
  77.           // Snapping in the off position:
  78.           if(options.snap && tmp < options.snap){
  79.             tmp = 0;
  80.           }
  81.  
  82.           // This would suggest we are at an end position;
  83.           // we need to block further rotation.
  84.           if(Math.abs(tmp - lastDeg) > 180){
  85.             return false;
  86.           }
  87.  
  88.           currentDeg = tmp;
  89.           lastDeg = tmp;
  90.  
  91.           knobTop.css('transform','rotate('+(currentDeg)+'deg)');
  92.           options.turn(currentDeg/359);
  93.         });
  94.  
  95.         doc.on('mouseup.rem',function(){
  96.           knob.off('.rem');
  97.           doc.off('.rem');
  98.  
  99.           // Saving the current rotation
  100.           rotation = currentDeg;
  101.  
  102.           // Marking the starting degree as invalid
  103.           startDeg = -1;
  104.         });
  105.  
  106.       });
  107.     });
  108.   };
  109.  
  110. })(jQuery);

更多信息请参考:http://ksylvest.github.io/jquery-gridly/

knobKnob - 旋钮控制按钮

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

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

发表回复

热销模板

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

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