原生js 视频放大镜 (等同于图片放大镜)

注意:记得打开flash 打开flash方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/video.js/5.3.0/video-js.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/video.js/5.3.0/video.min.js"></script>
    <title>视频放大镜</title>
    <style>
        html,body{
            padding: 0;
            margin: 0;
        }
        .con{
            position: relative;
            width: 400px;
            height: 300px;
            background-color:aquamarine;
            overflow: hidden;
        }
        .mover{
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 75px;
            border:1px solid red;
            cursor: pointer;
            background-color: rgba(255,255,255,.2)
        }
        .adddel{
            position: absolute;
            left: 0;
            bottom: 0px;
            display: flex;
            justify-content:space-between;
            width: 100%;
        }
        .adddel div{
            cursor: pointer;
            margin: 4px 10px;
        }
        .conshow{
            margin-top: 10px;
            display: none;
            position: relative;
            width: 400px;
            height: 300px;
            overflow: hidden;
            background-color: aquamarine
        }
        .videocon{
            position: absolute;
            top: 0;
            left: 0;
            background-color: red
        }
    </style>
</head>
<body>
    <div class="con">
        <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" style="width: 100%;height:100%"
            poster="http://vjs.zencdn.net/v/oceans.png">
            <source src="rtmp://58.200.131.2:1935/livetv/hunantv" type="rtmp/flv">
        </video>
        <div class="mover">
            <div class="adddel">
                <div class="add">add</div>
                <div class="reduce">reduce</div>
            </div>
        </div>
        
    </div>
    
    <div class="conshow"><div class="videocon"></div></div>

    <script>
        var player = videojs('example_video_1',{})
        player.play();
        var _videoStr = `<video id="example_video_2" class="video-js vjs-default-skin"  preload="none" 
                        style="width:100%;height:100%;"
                        poster="http://vjs.zencdn.net/v/oceans.png">
                        <source src="rtmp://58.200.131.2:1935/livetv/hunantv" type="rtmp/flv">
                        </video>`
        var con = document.querySelector('.con');
        var mover = document.querySelector('.mover');
        var conshow = document.querySelector('.conshow');
        var videocon = document.querySelector('.videocon');
        var add = document.querySelector('.add');
        var reduce = document.querySelector('.reduce');

        // 倍率  con/mover
        var w_dynameter,h_dynameter;

        add.onclick = function(){
            var _movW = mover.offsetWidth-2;
            var _movH = mover.offsetHeight-2;
            var _wh = _movW/_movH;
            console.log(_movH*_wh)
            mover.style.width = _movW + 10*_wh +2 +'px';
            mover.style.height = _movH + 10 +2 +'px';
        }
        reduce.onclick = function(){
            var _movW = mover.offsetWidth-2;
            var _movH = mover.offsetHeight-2;
            var _wh = _movW/_movH;

            mover.style.width = _movW - 10*_wh +2 +'px';
            mover.style.height = _movH - 10 +2 +'px';
        }
        
        var player2;
        con.onmouseover = function(){
            mover.style.display = 'block'
            w_dynameter = con.offsetWidth/(mover.offsetWidth-2)//去掉边框
            h_dynameter = con.offsetHeight/(mover.offsetHeight-2)  
        }
        con.onmouseout = function(){
            mover.style.display = 'none'
        }
        moverCon()
        function moverCon(){ //mover在con拖动
            var _move = false;
            var _x,_y,_top,_left;
            mover.onmousehover = function(){
                conshow.style.display = 'none'
            }
            mover.onmousedown = function(down){
                _move = true;
                mover.style.cursor = 'move';
                _x = down.clientX;
                _y = down.clientY;
                _top = mover.offsetTop;
                _left = mover.offsetLeft;
               
            }
            mover.onmousemove = function(move){           
                if(_move){
                    var mov_top = move.clientY - _y + _top;
                    var mov_left = move.clientX - _x + _left
                    if(mov_top <=0){
                        mov_top = 0
                    }
                    var _htop = con.offsetHeight-mover.offsetHeight
                    if(mov_top >= _htop){
                        mov_top = _htop
                    }
                    if(mov_left <=0){
                        mov_left = 0
                    }
                    var _wleft = con.offsetWidth-mover.offsetWidth
                    if(mov_left >=_wleft){
                        mov_left = _wleft
                    }
                    mover.style.top = mov_top +'px';
                    mover.style.left = mov_left +'px';

                    // var _video2 = document.querySelector('#example_video_2');
                    videocon.style.top = -mov_top*h_dynameter+'px';
                    videocon.style.left = -mov_left*w_dynameter+'px';
                }
            }
            mover.onmouseup = function(up){
                _move = false;
                mover.style.cursor = 'auto';
                _top = up.offsetTop;
                _left = up.offsetLeft;
            }
            mover.onmouseover = function(over){
                conshow.style.display = 'block' 
                var _conshowW = conshow.clientWidth;
                var _conshowH = conshow.clientHeight;             
                videocon.innerHTML = _videoStr;
                player2 = videojs('example_video_2',{})
                player2.play();
                videocon.style.width = _conshowW*w_dynameter +'px';
                videocon.style.height = _conshowH*h_dynameter +'px';

                console.log(videocon,w_dynameter,h_dynameter)
                console.log("w",_conshowW*w_dynameter,"h",_conshowH*h_dynameter)
                _move = false;
                mover.style.cursor = 'auto';
                _top = over.offsetTop;
                _left = over.offsetLeft;
            }
            mover.onmouseout = function(over){
                player2.dispose()
                videocon.innerHTML = '';
                conshow.style.display = 'none'

            }
        }
        
        // conshowvideo()

        function createvideo(){

        }
        
    </script>
</body>
</html>

效果图
在这里插入图片描述
bug 2个视频不同步 如果有更好的方法 请大神指教

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的原生JS实现放大镜功能的代码: HTML: ``` <div class="container"> <img src="image.jpg" alt="product image" id="product-image"> <div class="magnifier"></div> </div> ``` CSS: ``` .container { position: relative; } .magnifier { position: absolute; width: 200px; height: 200px; border: 1px solid #ccc; display: none; pointer-events: none; background-repeat: no-repeat; background-size: 400px 400px; background-position: 0 0; } ``` JS: ``` const container = document.querySelector('.container'); const magnifier = document.querySelector('.magnifier'); const productImage = document.querySelector('#product-image'); container.addEventListener('mousemove', e => { const containerRect = container.getBoundingClientRect(); const x = e.clientX - containerRect.left; const y = e.clientY - containerRect.top; const magnifierSize = 200; const imageWidth = productImage.width; const imageHeight = productImage.height; const ratioX = imageWidth / containerRect.width; const ratioY = imageHeight / containerRect.height; const bgPosX = -x * ratioX + magnifierSize / 2; const bgPosY = -y * ratioY + magnifierSize / 2; magnifier.style.display = 'block'; magnifier.style.left = `${x - magnifierSize / 2}px`; magnifier.style.top = `${y - magnifierSize / 2}px`; magnifier.style.backgroundImage = `url(${productImage.src})`; magnifier.style.backgroundPosition = `${bgPosX}px ${bgPosY}px`; }); container.addEventListener('mouseleave', () => { magnifier.style.display = 'none'; }); ``` 这段代码会在鼠标移动到图片上时,显示一个放大镜,并在放大镜内显示鼠标所在位置的图片放大后的部分。当鼠标移开图片时,放大镜会隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值