Home Web Front-end JS Tutorial Clock effect implemented by jQuery+css (compatible with all browsers)_jquery

Clock effect implemented by jQuery+css (compatible with all browsers)_jquery

May 16, 2016 pm 03:17 PM
css jquery clock

The example in this article describes the clock effect implemented by jQuery+css. Share it with everyone for your reference, the details are as follows:

The screenshot of the running effect is as follows:

There is not much modification here, the function is simply implemented. In addition, the setTimeout method of js is used. When the time is long, there will be a certain delay. It is recommended to execute it every few minutes. Clock calibration! Haha, there are errors. Anyway, I didn’t correct it for you.

The specific code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title></title>
  <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
  <script type="text/javascript" >
    var addRadian = Math.PI / 30;
    var wrapper = null;
    var minutes = 0;
    var hours = 0;
    var secondsLineLength = 14;
    var secondLineLength = 20;
    function cloneObj(obj1) {
      var tempObj = {};
      for (var i in obj1) {
        if (obj1.hasOwnProperty(i)) {
          tempObj[i] = obj1[i];
        }
      }
      return tempObj;
    }
    function createMinute(po, r, text) {
      var minute = [];
      minute.push('<div class="minute" style="left:');
      minute.push(po.x);
      minute.push('px; top:');
      minute.push(po.y);
      minute.push('px;" >');
      minute.push(text);
      minute.push('</div>');
      wrapper.append(minute.join(''));
    }
    function createHour(po, r, text) {
      var minute = [];
      minute.push('<div class="hour" style="left:');
      minute.push(po.x);
      minute.push('px; top:');
      minute.push(po.y);
      minute.push('px;" >');
      minute.push(text);
      minute.push('</div>');
      wrapper.append(minute.join(''));
    }
    function initSeconds(text, center, range) {
      var now_seconds = new Date().getSeconds();
      now_seconds = now_seconds > 0 &#63; now_seconds - 1 : 0;
      for (var i = 0; i < secondsLineLength + 1; i++) {
        createFlower(center, '●', range, (i + 1) * secondLineLength, Math.PI / 2 + (now_seconds) * addRadian, true, i == secondsLineLength &#63; true : false);
      }
    }
    function initMinutes(r, text, center) {
      var x = 0,
        y = 0;
      for (var i = 0; i < 60; i++) {
        x = center.x - Math.cos(Math.PI / 2 + i * addRadian) * (r + secondLineLength);
        y = center.y - Math.sin(Math.PI / 2 + i * addRadian) * (r + secondLineLength); 
        createMinute({x: x,y: y}, r, text);
      }
      minutes = new Date().getMinutes();
      waldedMinute(minutes);
    }
    function initHours(r, text, center) {
      var x = 0,
        y = 0;
      for (var i = 0; i < 60; i+=5) {
        x = center.x - Math.cos(Math.PI / 2 + i * addRadian) * (r + secondLineLength);
        y = center.y - Math.sin(Math.PI / 2 + i * addRadian) * (r + secondLineLength);
        createHour({ x: x, y: y }, r, text);
      }
      hours = new Date().getHours();
      waldedHour(hours);
    }
    function waldedMinute(index) {
      var index = Math.floor((index % 60)) > 0 &#63; Math.floor((index % 60)) + 1 : 0;
      wrapper.find(".minute:lt(" + index + ")").css('color', "green");
      if (index > 0) {
        wrapper.find(".minute:eq(0)").css('color', '#DDDDDD');
      }
    }
    function waldedHour(index) {
      var index = Math.floor((index%12)) > 0 &#63; Math.floor((index%12)) + 1:0;
      wrapper.find(".hour:lt(" + index + ")").css('color', "green");
      if(index > 0) {
        wrapper.find(".hour:eq(0)").css('color', '#494949');
      }
    }
    function animation(obj, r, radian, range, center, text, last) {
      logNowTime();
      radian += addRadian;
      var x = center.x - Math.cos(radian) * r;
      var y = center.y - Math.sin(radian) * r;
      obj.css({ "left": x, "top": y });
      if (last && radian > Math.PI * 5 / 2 - 0.1) {
        radian = Math.PI / 2;
        minutes++;
        if (minutes < 60) {
        } else {
          if (minutes % 60 == 0) {
            hours++;
            if (hours % 12 != 0) {
            } else {
              wrapper.find(".hour").css('color', "#494949");
            }
            waldedHour(hours);
          } else {
            wrapper.find(".minute").css('color', "#DDDDDD");   
          }
        }
        waldedMinute(minutes); 
      }
      setTimeout(function () {
        animation(obj, r, radian, range, center, text, last);
      }, 1000);
    }
    function createFlower(center, text, range, r, radian, autoAnimate, last) {
      var flower = [];
      flower.push('<div class="second"');
      flower.push(' style="left:');
      flower.push(center.x);
      flower.push('px; top:');
      flower.push(center.y);
      flower.push('px;');
      flower.push(autoAnimate &#63; '" >' : 'color:red;" >');
      flower.push(text);
      flower.push('</div>');
      flower = $(flower.join(''));
      flower.appendTo(wrapper);
      //var r = (index + 1) * secondLineLength;
      if (autoAnimate) {
        animation(flower, r, radian, range, center, text, last);
      }
    }
    // 查看当前时间
    function logNowTime() {
      var date = new Date(),
        hour = date.getHours(),
        minute = date.getMinutes(),
        second = date.getSeconds();
      hour = hour < 10 &#63; "0" + hour : hour;
      minute = minute < 10 &#63; "0" + minute : minute;
      second = second < 10 &#63; "0" + second : second;
      $("#time").html("当前时间-" + hour + ":" + minute + ":" + second);
    }
    $(document).ready(function () {
      wrapper = $("#wrapper"),
        width = wrapper.width(),
        height = wrapper.height(),
        offLeft = parseInt(wrapper.offset().left),
        range = {
          x: offLeft,
          y: 0,
          x1: offLeft + width,
          y1: height
        },
        center = {
          x: Math.round(width / 2) + offLeft,
          y: Math.round(height / 2)
        };
      initHours(secondLineLength * secondsLineLength + 40, '●', center);
      initMinutes(secondLineLength * secondsLineLength + 20, '●', center);
      initSeconds('●', center, range);
    });  
  </script>
  <style type="text/css" >
    body { margin:0; padding:0; }
    #wrapper { margin:0 auto; width:1000px; height:780px; background:black; }
    .second { width:12px; height:12px; position:absolute; text-shadow:1px 1px 1px green; color:Green; } 
    .minute { color:#DDDDDD; position:absolute;}
    .hour { color:#494949; position:absolute;}
    #time { font-size:30px; line-height:30px; text-shadow:2px 2px 2px green; text-align:center; }
  </style>
</head>
<body>
<div id="time"></div>
<div id="wrapper" >
</div>
</body>
</html>

Copy after login

Readers who are interested in more content related to jQuery special effects can check out the special topics on this site: "Summary of common classic special effects of jQuery" and "Summary of jQuery animation and special effects usage"

I hope this article will be helpful to everyone in jQuery programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles