简体   繁体   English

HTML元素稍微偏离中心

[英]HTML elements slightly off centre

I'm using the Leaflet Marker Cluster plugin with custom div elements to represent a cluster icon. 我正在使用带有自定义div元素的Leaflet Marker Cluster插件来表示群集图标。 When a group of markers on a map is clustered, a cluster icon appears in place of them. 当地图上的一组标记聚集在一起时,将出现一个聚集图标代替它们。 Hovering over the cluster icon reveals a polygon shape on the map indicating the area the cluster icon covers. 将鼠标悬停在群集图标上会在地图上显示一个多边形,指示群集图标覆盖的区域。 I want the icons to sit in the middle of the polygon. 我希望图标位于多边形的中间。 However, they all seem to be slightly to the bottom right of the center. 但是,它们似乎都稍微位于中心的右下角。 Here's an image to show what I mean. 这是一张图片,显示我的意思。 The black lined shapes indicate the polygon, the red lines where the middle of the polygon is and the blue circles are the cluster icons. 黑色线条表示多边形,红色线条表示多边形的中间,蓝色圆圈表示聚类图标。

Pic below : 图片如下:

在此处输入图片说明

They blue circles are all slightly to the bottom right of the centre in my application. 在我的应用程序中,它们的蓝色圆圈都略微位于中心右下角。 The CSS for my cluster icons is: 我的群集图标的CSS是:

.marker-cluster-div {
    width: 40px;
    height: 40px;
    font-size: 12px;
    border-radius: 50%;
    color: black;
    font-weight: bold;
    text-align: center;
    vertical-align: middle;
    line-height: 40px;
}

Is there any easy way to force an element to the left and up from where it was originally positioned? 是否有任何简单的方法可以将元素从最初放置的位置向左或向上推?

Make sure the parent element of .marker-cluster-div has a relative position and apply this css: 确保.marker-cluster-div的父元素具有相对位置,并应用以下CSS:

.marker-cluster-div {
    position: absolute;
    width: 40px;
    height: 40px;
    top: 50%;
    margin-top: -20px;
    left: 50%;
    margin-left: -20px;
    font-size: 12px;
    border-radius: 50%;
    color: black;
    font-weight: bold;
    text-align: center;
    vertical-align: middle;
    line-height: 40px;
}

The proper way to use custom markers in the Clustermarker plugin is by using the iconCreateFunction which can passed in the options when initializing your Clustermarker: 在Clustermarker插件中使用自定义标记的正确方法是使用iconCreateFunction,它可以在初始化Clustermarker时在选项中传递:

iconCreateFunction: function (cluster) {
    var markers = cluster.getAllChildMarkers();
    var n = 0;
    for (var i = 0; i < markers.length; i++) {
        n += markers[i].number;
    }
    return L.divIcon({ html: n, className: 'mycluster', iconSize: L.point(40, 40) });
}

That way the plugin will do you heavy lifting and you can do fancy stuff based on the amount of childMarkers present within that cluster. 这样,插件将为您带来繁重的工作,并且您可以根据该集群中存在的childMarkers的数量来做一些花哨的事情。 See the documentation and a working example 请参阅文档工作示例

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

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