简体   繁体   English

使用CSS3动画的脉动传单标记

[英]Pulsating Leaflet marker using CSS3 animations

I want to create a custom pulsating map marker icon on a Leaflet map. 我想在Leaflet地图上创建一个自定义的脉动地图标记图标。 For learning purposes I don't want to use third party plugins. 出于学习目的,我不想使用第三方插件。

I am using the following CSS code for creating the 'pulsating'-animation: 我正在使用以下CSS代码来创建'脉动'动画:

.gps_ring {
    border: 3px solid #999;
    -webkit-border-radius: 30px;
    height: 18px;
    width: 18px;    
    -webkit-animation: pulsate 1s ease-out;
    -webkit-animation-iteration-count: infinite;     
}   
@-webkit-keyframes pulsate {
    0% {-webkit-transform: scale(0.1, 0.1); opacity: 0.0;}
    50% {opacity: 1.0;}
    100% {-webkit-transform: scale(1.2, 1.2); opacity: 0.0;}
}

I am using Leaflet's DivIcon in order to change the visualization of a marker (referencing the CSS class above): 我正在使用Leaflet的DivIcon来改变标记的可视化(引用上面的CSS类):

// Define an icon called cssIcon
var cssIcon = L.divIcon({
    // Specify a class name we can refer to in CSS.
    className: 'gps_ring'
});

// Create markers and set their icons to cssIcon
L.marker([50.5, 30.5], {icon: cssIcon}).addTo(map);

This approach doesn't work at the moment. 这种方法目前不起作用。 The animated marker icon always shows up on the top left corner of the map. 动画标记图标始终显示在地图的左上角。 It seems that the transformation (scale) breaks the current marker location: 似乎转换(比例)打破了当前的标记位置:

在此输入图像描述

BTW I am using Chrome 44.x on Windows 7 and Yosemite. 顺便说一下,我在Windows 7和Yosemite上使用Chrome 44.x.

I have created a minimal example here: 我在这里创建了一个最小的例子:

http://jsfiddle.net/christianjunk/q69qx45c/1/ http://jsfiddle.net/christianjunk/q69qx45c/1/

What is going wrong? 出了什么问题? Why does the animation breaks the marker's map position? 为什么动画会破坏标记的地图位置?

After investigating for some more time I found a way to solve the problem: 在调查了一段时间后,我找到了解决问题的方法:

I changed the CSS code slighlty: 我改变了CSS代码slighlty:

.css-icon {

}

.gps_ring { 
    border: 3px solid #999;
     -webkit-border-radius: 30px;
     height: 18px;
     width: 18px;       
    -webkit-animation: pulsate 1s ease-out;
    -webkit-animation-iteration-count: infinite; 
    /*opacity: 0.0*/
}

@-webkit-keyframes pulsate {
        0% {-webkit-transform: scale(0.1, 0.1); opacity: 0.0;}
        50% {opacity: 1.0;}
        100% {-webkit-transform: scale(1.2, 1.2); opacity: 0.0;}
}

And I changed the instantiation of my DivIcon class: 我改变了DivIcon类的实例化:

// Define an icon called cssIcon
var cssIcon = L.divIcon({
  // Specify a class name we can refer to in CSS.
  className: 'css-icon',
  html: '<div class="gps_ring"></div>'
  // Set marker width and height
  ,iconSize: [22,22]
  // ,iconAnchor: [11,11]
});

The following did the trick: I am now using the inner html to run the CSS animation. 以下是诀窍:我现在使用内部html来运行CSS动画。 This will preserve the marker's location. 这将保留标记的位置。 Also note the iconSize attribute, which has the total size of the icon after the transformation ( 21 px =~ 18px x 1.2). 另请注意iconSize属性,该属性具有转换后图标的总大小(21 px = ~18px x 1.2)。 Setting it this way centers the animated circle at the coordinate: 以这种方式设置将动画圆圈置于坐标中心:

Working example: http://jsfiddle.net/christianjunk/waturuoz/ 工作示例: http//jsfiddle.net/christianjunk/waturuoz/

在此输入图像描述

I still couldn't figure out, why my first approach wasn't working. 我仍然无法弄清楚,为什么我的第一种方法不起作用。

Your original approach didn't work because you used transform (which Leaflet also uses to place it's markers). 您的原始方法不起作用,因为您使用了变换(Leaflet也使用它来放置它的标记)。

Example of a Leaflet placement which is overwritten: 被覆盖的传单放置示例:

transform: translate3d(499px, 604px, 0px);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM