Bonjour,

J'utilise jQuery pour positionner des points sur une map. Cependant, au premier chargement de la page, il arrive que les markers ne soient pas bien positionnés ne tenant pas compte de la valeur X ou Y. Mais en rechargant la page cela fonctionne.
Quel pourrait être le problème.

De plus, je ne pense pas que mon script soit reellement optimisé, je pense qu'il doit y avoir une facon plus simple de gerer la positionnement ainsi que le positionnement on.resize...
Vous avez une idée de comment optimiser cela ?

Merci d'avance


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
/* === INTERACTIVE MAP === */
 
$currentMapIndicator = null;
$isLocationMarksAnimated = false;
 
$offices = {
    "belgium": {
        "name": "Belgium",
        "address": "Address",
        "state": "State",
        "region": "EU",
        "location": {
            "x": 480,
            "y": 145
        }
    },
    "florida": {
        "name": "Florida",
        "address": "Address",
        "state": "State",
        "region": "NA",
        "location": {
            "x": 251,
            "y": 220
        }
    },
    "pennsylvania": {
        "name": "Pennsylvania",
        "address": "Address",
        "state": "State",
        "region": "NA",
        "location": {
            "x": 270,
            "y": 191
        }
    },
    "hongkong": {
        "name": "China",
        "address": "Address",
        "state": "State",
        "region": "AS",
        "location": {
            "x": 785,
            "y": 224
        }
    },
    "india": {
        "name": "India",
        "address": "Address",
        "state": "Station Road, Goregaon-W Mumbai, India 400062",
        "region": "AS",
        "location": {
            "x": 685,
            "y": 240
        }
    },
    "singapore": {
        "name": "Singapore",
        "address": "865 Mountbatten Road #06-04, Katong Shopping Centre",
        "state": "State",
        "region": "AS",
        "location": {
            "x": 767,
            "y": 280
        }
    },
    "brazil": {
        "name": "Brazil",
        "address": "Address",
        "state": "State",
        "region": "SA",
        "location": {
            "x": 330,
            "y": 330
        }
    },
    "mexico": {
        "name": "Mexico",
        "address": "Privada del Encino #770, Colonia Lagrange",
        "state": "Monterrey, Nuevo Leon 66410",
        "region": "SA",
        "location": {
            "x": 200,
            "y": 235
        }
    }
}
 
function createMarkers() {
 
    $i = 1;
 
    for ($key in $offices) {
 
        $office = $offices[$key];
 
        if ($key == "belgium") {
            $("#worldmap").append('<i class="far fa-dot-circle fa-2x location_mark active" id="' + $key + '" data-title="'+ $office.name + '"></i>');
 
        } else {
            $("#worldmap").append('<i class="far fa-dot-circle fa-2x location_mark" id="' + $key + '" data-title="'+ $office.name + '"></i>');
        }
 
        $("#" + $key).css({
 
            top: $offices[$key].location.y / 462 * $("#worldmap").height(),
            left: $offices[$key].location.x / 1001 * $("#worldmap").width()
 
        });
 
        $("#" + $key).each(function() {
 
            $(this).delay(250 * $i).fadeIn(500);
 
        });
 
        $i++;
 
    }
 
}
 
 
function locateMarkers() {
 
    for ($key in $offices) {
 
        $("#" + $key).css({
 
            top: $offices[$key].location.y / 462 * $("#worldmap").height(),
            left: $offices[$key].location.x / 1001 * $("#worldmap").width()
 
        });
 
    }
 
}
 
 
$(document).ready(function() {
 
    createMarkers();
 
    $(".location_mark").on("click", function() {
 
        if ($(this).hasClass("active")) { return; }
 
        $currentMapIndicator = $(this);
 
        $office = $offices[this.id];
 
        $(".location_mark").removeClass("active");
        $currentMapIndicator.addClass("active");
 
    });
 
});
 
 
$(window).on("resize", function() {
 
    locateMarkers();
 
});