Home  >  Article  >  Web Front-end  >  How to make a responsive time picker using HTML, CSS and jQuery

How to make a responsive time picker using HTML, CSS and jQuery

王林
王林Original
2023-10-26 11:27:131043browse

How to make a responsive time picker using HTML, CSS and jQuery

How to use HTML, CSS and jQuery to make a responsive time picker

In modern web design, responsive design has become a very important element . The time picker is also an essential component in many websites and applications. This article will introduce you to how to use HTML, CSS and jQuery to make a responsive time picker, and will provide specific code examples for reference.

Step 1: Create a basic HTML structure

First, we need to create a basic HTML structure to accommodate the time selector. We can add a

tag in the tag, whose class attribute is "timepicker" (time picker), and set a text input box and an icon button. The overall code is as follows:
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" type="text/css" href="styles.css">
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="script.js"></script>
</head>

<body>
  <div class="timepicker">
    <input type="text" class="time-input" readonly>
    <i class="fas fa-clock"></i>
  </div>
</body>

</html>

Step 2: Create CSS styles

Next, we need to add some basic CSS styles to this time picker to make it look more beautiful and easy use. You can refer to the following code example:

.timepicker {
  position: relative;
  display: inline-block;
}

.time-input {
  padding: 6px 12px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.fa-clock {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  color: #666;
}

Step 3: Add jQuery event

Now, we need to use jQuery to handle the interactive function of the time picker. First, add the following code to the JavaScript file (script.js):

$(function() {
  $('.timepicker').on('click', function() {
    $('.time-input').focus();
  });
  
  $('.time-input').on('focus', function() {
    $(this).attr('type', 'time');
  });
  
  $('.time-input').on('blur', function() {
    $(this).attr('type', 'text');
  });
});

In the above code, we use jQuery to listen to the click event of the time selector and make the text input box focus. When the text input box gets focus, we change its type to "time" so that the user can use the time picker to select a time. When the text input box loses focus, we change its type back to "text".

Step 4: Add FontAwesome icon library

In order to make the time picker look more beautiful, we can use FontAwesome icon library. Simply add the following code to the

tag in the HTML file:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">

At this point, we have completed the production of a simple responsive time picker. The user can click the time input box to pop up the time selector and select the desired time. The selected time will be displayed in the time input box.

In addition, if you want to further increase the functionality and style of the time picker, you can modify the CSS style and jQuery events yourself.

Summary:

This article introduces how to use HTML, CSS and jQuery to make a simple responsive time picker. By adding basic HTML structure, CSS styles, and jQuery events, we can implement a user-friendly time picker. I hope this article will help you understand and create responsive time pickers.

The above is the detailed content of How to make a responsive time picker using HTML, CSS and jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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