mdui实现一个拖动时钟指针选择事件效果代码

代码语言:html

所属分类:选择器

代码描述:mdui实现一个拖动时钟指针选择事件效果代码

代码标签: 拖动 时钟 指针 选择 事件 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">
    <link type="text/css" rel="stylesheet" href="//repo.bfw.wiki/bfwrepo/css/mdui.min.css">

    <style>
        html, body {
          height: 100%;
        }
        body {
          margin: 0;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-direction: column;
          overflow-y: auto;
        }
        /*好看的更新动画*/
        mdui-replaceable {
          position: relative;
          display: block;
        }
        mdui-replaceable > div {
          box-sizing: border-box;
          width: 100%;
        }
        mdui-replaceable > .replacement-wrapper {
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          text-align: inherit;
        }
        /*选择器公用样式*/
        .picker {
          width: 330px;
          box-shadow: 0 39px 56px 4px rgba(0, 0, 0, .24);
          border-radius: 2px;
          overflow: hidden;
          background: white;
          /*控件颜色*/
          --preferred-color: #009688;
        }
        .picker > header {
          width: 100%;
          background: var(--preferred-color, #009688);
          color: white;
        }
        .picker > header button {
          text-align: left;
          box-sizing: border-box;
          border: none;
          padding: 0;
          background: none;
          color: rgba(255, 255, 255, .7);
          font-family: Roboto, -apple-system, PingFang SC, Noto Sans CJK SC, sans-serif;
          font-size: 16px;
          font-weight: 500;
          height: 16px;
          transition: all .3s cubic-bezier(0.2, 0.9, 0.4, 1);
        }
        .picker > header button.picker-toggle-active {
          color: white;
        }
        .picker > footer > button {
          color: var(--preferred-color, #009688)!important; /*MDUI对按钮的颜色设置会覆盖此属性*/
        }
        /*时间选择器*/
        .picker.time-picker {
          user-select: none;
        }
        .picker.time-picker > header {
          box-sizing: border-box;
          padding: 28px 48px 23px;
          display: flex;
          align-items: center;
          justify-content: flex-end;
          color: rgba(255, 255, 255, .7);
        }
        .picker.time-picker .time-picker--time-hour-switch, .picker.time-picker .time-picker--time-hour-switch > button {
          font-size: 60px;
          height: 45px;
          line-height: 20px;
          font-weight: 400;
        }
        .picker.time-picker .time-picker--am-pm-switch {
          margin-left : 24px;
          display: flex;
          flex-direction: column;
          justify-content: center;
        }
        .picker.time-picker .time-picker--am-pm-switch > button {
          height: 12px;
          line-height: 20px;
          overflow: visible;
        }
        .picker.time-picker .time-picker--am-pm-switch > button+button {
          margin-top: 11px;
        }
        .picker.time-picker .time-picker--selector {
          position: relative;
          box-sizing: border-box;
          height: 296px;
          width: 296px;
          border-radius: 50%;
          background: #EEEEEE;
          margin: 33px auto 8px;
          user-select: none;
          padding: 14px;
        }
        .picker.time-picker .time-picker--selection-wrapper {
          position: absolute;
          z-index: 2;
          width: 268px;
          height: 268px;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: flex-start;
          transform: rotate(var(--selection-rotation));
        }
        .picker.time-picker .time-picker--selection-wrapper > span {
          transform: rotate(calc( var(--selection-rotation) * -1 ));
          font-size: 16px;
          color: rgba(0, 0, 0, .87);
          letter-spacing: .1px;
        }
        .picker.time-picker :is(.time-picker--indicator, .time-picker--indicator-inverted) {
          position: absolute;
          left: 0;
          top: 0;
          right: 0;
          bottom: 0;
          padding: 3px;
          transform: rotate(var(--indicator-rotation));
          display: flex;
          flex-direction: column;
          align-items: center;
          transition: transform .3s cubic-bezier(0.2, 0.9, 0.4, 1);
        }
        .picker.time-picker .time-picker--selector:is(.time-picker--active, :active) :is(.time-picker--indicator, .time-picker--indicator-inverted) {
          transition: none; /*按着鼠标调整时,不应该有过渡,这会让钟表指针的运动看起来迟钝、不跟手*/
        }
        .picker.time-picker .time-picker--hand {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          background: var(--preferred-color);
          filter: invert(1);
          display: flex;
          align-items: center;
          justify-content: center;
        }
        .picker.time-picker .time-picker--hand.time-picker--display-black-dot::before {
          content: '';
          height: 4px;
          width: 4px;
          background: black;
          border-radius: 50%;
          filter: invert(1);
        }
        .picker.time-picker .time-picker--needle {
          width: 2px;
          height: 102px;
          background: var(--preferred-color);
        }
        .picker.time-picker .time-picker--axis { 
          height: 6px;
          width: 6px;
          background: var(--preferred-color);
          border-radius: 50%;
        }
        .picker.time-picker .time-picker--indicator-inverted {
          z-index: 3;
        }
        .picker.time-picker .time-picker--indicator-inverted::before {
          content: ' ';
          display: block;
          height: 40px;
          width: 40px;
          border-radius: 50%;
          backdrop-filter: invert(1);
        }
        /*24小时*/
        .picker.time-picker.time-picker--24-hours > header {
          justify-content: center;
        }
    </style>



</head>

<body>
    <div class="picker time-picker">
        <!--dialog 元素有自己的样式,而且默认会藏起来自己,使用不便-->
        <header>
            <div class="time-picker--time-hour-switch">
                <button id="hour" class="mdui-ripple">-</button>:<button id="minute" class="mdui-ripple">-</button>
            </div>
            <div class="time-picker--am-pm-switch">
                <button id="am" class="mdui-ripple">-</button>
                <button id="pm" class="mdui-ripple">-</button>
            </div>
        </header>
        <!--mdui-replaceable 的加入,使HTML结构很复杂,所以有必要用class来简化CSS选择器,不然选择器一堆“>”真是看不下去-->
        <main class="time-picker--selector">
            <div class="time-picker--indicator">
                <div class="time-picker--hand"></div>
                <div class="time-picker--needle"></div>
                <div class="time-picker--axis"></div>
            </div>
            <mdui-replaceable></mdui-replaceable>
            <div class="time-picker--indicator-inverted"></div>
        </main>
        <footer class="mdui-dialog-actions">
            <button class="mdui-btn mdui-ripple" id="cancel">-</button>
            <button class="mdui-btn mdui-ripple" id="confirm">-</button>
        </footer>
    </div>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/mdui.min.js"></script>
    <script>
        const $ = mdui.$;
        
        //本地化信息,可自己修改
        const localization = {
          weekdays: "Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(","),
          weekdayAbbreviations: "SMTWTFS".split(""), //为了减少后边检测这是数组还是字符串的工作量(因为字符串没有map这样的方法),这里直接做成数组
          months: "January,February,March,April,May,June,July,August.........完整代码请登录后点击上方下载按钮下载查看

网友评论0