可拖动的进度条

可拖动-进度条

div和h3/h4组合生成可拖动的进度条

主要是通过设置空白内容的div的背景色和内置的占比内容的颜色及比例实现视觉效果上的可拖动的进度条

基本的HTML代码如下:

<div class = "father-div">
    <h3 style="width:50%;"></h3>
    <h4></h4>
</div>
    温度:<span>50</span>

father-div 代表的是包含占比进度条以及拖动显示的进度条的父容器
h3代表是占比拖动条
h4代表的是鼠标在进度条上移动时提示性的进度条移动
温度后的数字显示当前进度
基本的CSS设置如下:

.father-div {
    float:left;
    width:200px;
    height:10px;
    font-size:0;
    line-height:0;
    background:#f2f2f2;
    border:1px solid #dedede;
    position:relative;
    margin-top:10px;
    margin-right:10px;
    cursor:pointer;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
}
.father-div h3, .father-div h4{
    margin : 0;
    position:absolute;
    left:-1px;
    top:-1px;
    height:10px;
    font-size:0;
    line-height:0;
    width:10%;
    background:#a3d9f4;
    border:1px solid #187aab;
    z-index:99;
    -moz-border-radius:6px;
    -webkit-border-radius:6px;
    border-radius:6px;
}
.father-div h4 { 
    border:1px solid #91cdea; 
    z-index:88;
}

按照如上的css设计,div囊括h3和h4标签
下面设计当鼠标的移动和点击时修改进度条的内容

    $(".father-div").on("mousemove", function(e){
            var $mouse = e.pageX - $(this).offset().left;
            var $span = Math.round($mouse/2);
            $(this).find('h4').stop().animate({width:$span + '%'}, 50);
            $(this).next('span').text($span);
        }).on('mouseleave', function(){
            $(this).find('h4').stop().animate({width:'10%'}, 50);
            var $mousex = $(this).find('h3').width();
            var $spanx = Math.round($mousex/2);  
            if($spanx >= 40){
                $(this).next('span').text("过热");
            }else{
                $(this).next('span').text($spanx);
            }
        }).on("click", function(e){
            var $mouse = e.pageX - $(this).offset().left;
            var $span = Math.round($mouse/2);
            $(this).find('h3').stop().animate({width:$span+'%'}, 100);
            if($span >= 40){
                $(this).next('span').css("color","red").text('过热');
            }else{
                $(this).next('span').css("color","white").text($span);
            }
        });

对于这一句var spanx=Math.round( mousex/2); 主要是把200px长度的div父容器等分为100份(200/2),当移动鼠标的时候是按照+1的步长递增

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现可拖动的24小时进度条,可以在上面的代码基础上进行一些修改。具体的实现方法如下: HTML代码: ```html <div class="progress-bar"> <div class="progress"></div> <div class="handle"></div> </div> ``` CSS代码: ```css .progress-bar { width: 100%; height: 20px; background-color: #ddd; position: relative; cursor: pointer; } .progress { width: 0; height: 100%; background-color: #007bff; position: absolute; top: 0; left: 0; } .handle { width: 10px; height: 10px; background-color: #007bff; position: absolute; top: 50%; left: 0; transform: translate(-50%, -50%); border-radius: 50%; cursor: pointer; } ``` JavaScript代码: ```javascript function updateProgressBar() { var now = new Date(); var h = now.getHours(); var m = now.getMinutes(); var s = now.getSeconds(); var totalSeconds = (h * 60 * 60) + (m * 60) + s; var percentage = (totalSeconds / 86400) * 100; var progressBar = document.querySelector(".progress"); progressBar.style.width = percentage + "%"; } function handleDrag(e) { var progressBar = document.querySelector(".progress"); var progressbarRect = progressBar.getBoundingClientRect(); var offsetX = e.clientX - progressbarRect.left; var percentage = (offsetX / progressbarRect.width) * 100; progressBar.style.width = percentage + "%"; } var handle = document.querySelector(".handle"); handle.addEventListener("mousedown", function() { document.addEventListener("mousemove", handleDrag); }); document.addEventListener("mouseup", function() { document.removeEventListener("mousemove", handleDrag); }); updateProgressBar(); setInterval(updateProgressBar, 1000); ``` 在这个例子中,我们在HTML代码中添加了一个名为“progress”的div元素来表示进度条,以及一个名为“handle”的div元素来表示拖动手柄。在CSS代码中,我们添加了一些样式来实现拖动手柄的外观和样式。在JavaScript代码中,我们定义了一个名为handleDrag的函数,该函数会在鼠标移动时被调用,用于计算拖动手柄的位置,并更新进度条的宽度。我们还添加了mousedown和mouseup事件监听器,用于启用和禁用拖动手柄的功能。同时,我们也更新了updateProgressBar函数,以便在拖动手柄时也能够自动更新进度条的宽度。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值