【问题标题】:Radar scanner effect using jquery使用 jquery 的雷达扫描仪效果
【发布时间】:2017-02-15 22:11:39
【问题描述】:

我正在尝试找出一个可能的解决方案,了解如何使用 jquery 和 css 创建雷达扫描仪效果。本质上,一个半透明的三角形会围绕 div 的中点旋转。这可能与 jquery 或我应该诉诸其他手段?我更喜欢不使用动画 gif。

【问题讨论】:

  • 可以选择 CSS3 吗?这在 IE8 中是做不到的,在其他浏览器的纯 CSS 中应该相当容易。
  • 您应该解释一下您已经尝试过的操作...可以结合使用 jQuery/SVG 动画技术...这可能会有所帮助:stackoverflow.com/questions/2995643/…
  • 是的,如果你真的想,jQuery 可以控制 CSS3 转换。
  • 任何事情都是一种选择。

标签: jquery animation


【解决方案1】:

$(function() {

  var $rad = $('#rad'),
      $obj = $('.obj'),
      deg = 0,
      rad = $rad.width() / 2;

  $obj.each(function(){
    var pos = $(this).data(),
        getAtan = Math.atan2(pos.x-rad, pos.y-rad),
        getDeg = (-getAtan/(Math.PI/180) + 180) | 0;
    // Read/set positions and store degree
    $(this).css({left:pos.x, top:pos.y}).attr('data-atDeg', getDeg);
  });

  (function rotate() {      
    $rad.css({transform: 'rotate('+ deg +'deg)'}); // Radar rotation
    $('[data-atDeg='+deg+']').stop().fadeTo(0,1).fadeTo(1700,0.2); // Animate dot at deg

    deg = ++deg % 360;      // Increment and reset to 0 at 360
    setTimeout(rotate, 25); // LOOP
  })();

});
#radar{
  position:relative;
  overflow:hidden;
  width:321px; height:321px;
  background:#222 url(http://i.stack.imgur.com/vY6Tl.png);
  border-radius: 50%;
}
#rad{
  position:absolute;
  width:321px;
  height:321px; background:url(http://i.stack.imgur.com/fbgUD.png);
}
.obj{
  background:#cf5;
  position:absolute;
  border-radius: 50%;
  width:4px; height:4px; margin:-2px;
  box-shadow:0 0 10px 5px rgba(100,255,0,0.5);
  opacity:0.2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="radar">
    <div id="rad"></div>
    <div class="obj" data-x="120" data-y="65"></div>
    <div class="obj" data-x="140" data-y="185"></div>
    <div class="obj" data-x="220" data-y="35"></div>
    <div class="obj" data-x="120" data-y="235"></div>
  </div>

基本旋转循环:

<div id="radar">
    <div id="rad"></div>
</div>

CSS:

#radar{
  position:relative;
  width:321px;
  height:321px;
  background:#222 url(http://i.stack.imgur.com/vY6Tl.png);
  border-radius:320px;
}
#rad{
  position:absolute;
  width:321px;
  height:321px; background:url(http://i.stack.imgur.com/fbgUD.png);
}

jQuery:

$(function() {
  
    var $rad = $('#rad'),
        d = 0;

    (function rotate() {    
        $rad.css({ transform: 'rotate('+ d +'deg)'}); // apply CSS3
        setTimeout(function() {
            ++d;         // next degree
            rotate();    // recall function
        }, 25);          // every 25ms
    })();                // 1st start
  
});

【讨论】:

    【解决方案2】:

    CSS only demonstration

    HTML:

    <div id="radar">
        <div class="beacon" id="beacon"></div>
        <div class="beacon" id="beacon-75"></div>
        <div class="beacon" id="beacon-50"></div>
        <div class="beacon" id="beacon-25"></div>
        <div class="circle" id="circle-big"></div>
        <div class="circle" id="circle-medium"></div>
        <div class="circle" id="circle-small"></div>
        <div class="circle" id="dot"></div>
        <div id="vertical"></div>
        <div id="horizontal"></div>
    </div>
    

    CSS:

    #radar {
        position:relative;
        width:400px;
        height:400px;
        margin:20px auto;
        border:3px solid #0c0;
        background-color:#020;
        border-radius:50%;
    }
    #radar>* {position:absolute}
    .beacon {
        left:50%;
        top:50%;
        border-style:solid;
        border-width:8px 200px 8px 0;
        border-color:transparent;
        margin-top:-8px;
        transform-origin:0 50%;
    }
    #beacon {border-right-color:#0c0;animation:spin 2s 0s linear infinite}
    #beacon-75 {border-right-color:rgba(0,204,0,0.75);animation:spin 2s 0.03s linear infinite}
    #beacon-50 {border-right-color:rgba(0,204,0,0.5);animation:spin 2s 0.06s linear infinite}
    #beacon-25 {border-right-color:rgba(0,204,0,0.25);animation:spin 2s 0.09s linear infinite}
    .circle {
        left:50%;
        top:50%;
        border:1px solid #0c0;
        border-radius:50%;
    }
    #circle-big {
        width:300px;
        height:300px;
        margin:-150px;
    }
    #circle-medium {
        width:200px;
        height:200px;
        margin:-100px;
    }
    #circle-small {
        width:100px;
        height:100px;
        margin:-50px;
    }
    #dot {
        width:8px;
        height:8px;
        margin:-4px;
        background-color:#0c0;
    }
    #vertical {
        left:50%;
        top:0;
        bottom:0;
        border-left:1px solid #0c0;
    }
    #horizontal {
        top:50%;
        left:0;
        right:0;
        border-top:1px solid #0c0;
    }
    
    @keyframes spin {
        from {transform:rotate(0)}
        to {transform:rotate(360deg)}
    }
    

    请注意,要支持某些浏览器,您需要添加供应商前缀,但这本身适用于 IE10 和 Firefox。

    【讨论】:

    • Added Webkit prefix 虽然看起来不太对劲。第一个和第二个“信标”元素之间存在异常大的差距,尽管延迟是线性间隔的......
    • 很奇怪。一定是我的 Chrome 版本,我想我已经有一段时间没有更新了 XD
    • chrome 中没有旋转。
    • 稍后再访问,这是一个使用渐变而不是额外的 HTML 元素的更新版本,并且还显示了随机放置的点被清扫器“ping”。 jsfiddle.net/f9CtG/83
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2013-06-05
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多