账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    jQuery视频播放器小bug,拖动进度条小圆点闪现?
    43
    0

    小插件基本实现了视频播放,只那个小圆点很讨厌闪來闪去的。

    我的想法是阻止事件冒泡,亲测了下又不是这方面问题,目前很没有头绪。

    HTML布局里小圆点不是作为功能使用的,仅为装饰作用。求大神指教。

    html:

    <div class="box">
        <video src="media/35Q888piCCPd.mp4" controls id="vd"></video>
        <div class="circle clearfix">
            <a href="javascript:;" class="cirBtn"></a>
            <div class="cirProgress"><span class="grayBar"></span><span class="redBar"><em class="dot"></em></span></div>
            <div class="cirTime"><span>00:00</span> / <span>00:00</span></div>
        </div>
    </div>
    

    css:

    .box {
        margin: 0 auto;
        width: 980px;
    }
    .box video {
        display: block;
        width: 100%;
        height: 640px;
    }
    .circle {
        padding: 10px;
        background-color: #333;
    }
    .cirBtn {
        position: relative;
        float: left;
        width: 40px;
        height: 40px;
        border: 3px solid #fff;
        border-radius: 50%;
    }
    .cirBtn:after,.cirBtn:before {
        position: absolute;
        top: 50%;
        left: 50%;
        content: '';
    }
    .cirBtn:after {
        margin-top: -10px;
        margin-left: -5px;
        border-top: 10px solid transparent;
        border-bottom: 10px solid transparent;
        border-left: 15px solid #fff;
    }
    .cirPause:after,.cirPause:before {
        content: '';
        margin-top: -10px;
        width: 5px;
        height: 20px;
        background-color: #fff;
        border: 0 none;
    }
    .cirPause:after {
        margin-left: -9px;
    }
    .cirPause:before {
        margin-left: 4px;
    }
    
    .cirProgress {
        position: relative;
        float: left;
        margin-left: 15px;
        padding: 18px 0;
        width: 500px;
        height: 10px;
        cursor: pointer;
    }
    .cirProgress .grayBar,.cirProgress .redBar {
        position: absolute;
        top: 50%;
        left: 0;
        margin-top: -1.5px;
        height: 3px;
    }
    .cirProgress .grayBar {
        right: 0;
        background-color: #ccc;
    }
    .cirProgress .redBar {
        background-color: #ea4646;
    }
    .cirProgress .dot {
        position: absolute;
        top: 50%;
        right: 0;
        margin-top: -6px;
        margin-right: -6px;
        width: 12px;
        height: 12px;
        background-color: #ea4646;
        border-radius: 50%;
        cursor: pointer;
    }
    
    .cirTime {
        float: left;
        margin: 10px 0 0 15px;
        color: #fff;
        font-size: 16px;
        line-height: 26px;
    }
    

    jq:

    var $ele = $('#vd');
    var ele = $ele[0];
    var currenTime = 0;
    var duracTime = 0;
    
    ele.muted = true;
    // 当前时长和总时长
    ele.onloadedmetadata = function () {
        currenTime = ele.currentTime;
        duracTime = ele.duration;
        $('.cirTime span').eq(0).html(timeFormat(currenTime));
        $('.cirTime span').eq(1).html(timeFormat(duracTime));
    }
    $('.cirBtn').click(function () {
        if (ele.paused) {
            ele.play();
            $(this).addClass('cirPause');
        } else {
            ele.pause();
            $(this).removeClass('cirPause');
        }
    });
    
    function timeFormat(nums) {
        var seconds = Math.floor(nums % 60);
        if (seconds < 10) {
            seconds = '0' + seconds;
        }
        var minute = Math.floor(nums / 60);
        if (minute < 10) {
            minute = '0' + minute;
        }
        return minute + ':' + seconds;
    }
    
    // 进度条跟随
    $ele.on('timeupdate',function () {
        currenTime = ele.currentTime;
        duracTime = ele.duration;
        var percent = currenTime / duracTime * 100;
        $('.redBar').css('width',percent + '%');
        $('.cirTime span').eq(0).html(timeFormat(currenTime));
        if (ele.ended) {
            $('.cirBtn').removeClass('cirPause');
        }
    });
    
    // 进度条拖拽
    var lock = false;
    $('.cirProgress').on('mousedown',function (ev) {
        lock = true;
        changeCurrentTime(ev.offsetX);
    });
    $('.circle').on('mouseup',function (ev) {
        if (lock == true) {
            lock = false;
            changeCurrentTime(ev.offsetX);
        }
    }).on('mousemove',function (ev) {
        ev.stopPropagation();
        ev.preventDefault();
        if (lock == true) {
            changeCurrentTime(ev.offsetX);
        }
    })
    
    // 改变当前时间
    function changeCurrentTime(x) {
        var percent = x / $('.cirProgress').width() * 100;
        if (percent >= 100) {
            percent = 100;
        }
        if (percent <= 0) {
            percent = 0;
        }
        $('.redBar').css('width',percent + '%');
        ele.currentTime = percent * duracTime / 100;
        $('.cirTime span').eq(0).html(timeFormat(ele.currentTime));
    }
    3
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 紫夜天狼 普通会员 1楼

      在jQuery中,拖动进度条时,如果拖动的小圆点出现闪现现象,可能是由于以下几种原因:

      1. 约束高度设置过低:在拖动进度条时,如果进度条的高度设置过低,超出小圆点可视区域,就会导致小圆点在拖动过程中闪烁。可以通过设置position属性的值来调整进度条的高度,如position: relative; left: 50%; top: 50%; transform: translate(-50%, -50%),这将使进度条相对于其容器的位置进行缩放。

      2. 拖动事件处理不当:在拖动进度条时,如果没有正确地添加拖动事件监听器,或者处理错误的拖动事件,可能会导致小圆点在拖动过程中闪烁。可以使用mousedownmousemove事件来监听拖动操作,然后在事件处理函数中处理进度条的移动和缩放。例如:

      javascript $(document).ready(function() { $("#myProgress").draggable({ dragStart: function(event, ui) { ui.target.style.backgroundColor = "red"; }, drag: function(event, ui) { if (ui.position.left < 0 || ui.position.left + ui.helperRect.left > $("#myProgress").width()) { ui.helperRect.left = ui.position.left; } if (ui.position.top < 0 || ui.position.top + ui.helperRect.top > $("#myProgress").height()) { ui.helperRect.top = ui.position.top; } $(this).css("left", ui.position.left - 100 + "%"); $(this).css("top", ui.position.top - 100 + "%"); }, dragEnd: function(event, ui) { $(this).css("left", ui.position.left + 100 + "%"); $(this).css("top", ui.position.top + 100 + "%"); } }); });

      1. jQuery插件问题:在使用jQuery插件时,如<progress><span>等,如果有额外的样式设置,如透明度、颜色、宽度、高度等,可能会导致小圆点在拖动过程中闪烁。在这种情况下,可以尝试删除这些样式,或者在dragStartdragEnd事件处理函数中删除这些样式。

      2. jQuery动画设置问题:如果使用了jQuery动画,如animateanimate.css等,也可能会导致小圆点在拖动过程中闪烁。在这种情况下,可以检查动画设置是否正确,确保动画仅在拖动进度条时生效。

      以上是可能的解决方案,具体原因可能需要根据你的具体需求和代码情况进行排查和修改。如果问题依然存在,可能需要进行更深入的代码分析和测试,或者寻求专业的jQuery开发人员的帮助。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部