2016-05-17 38 views
-1

我與谷歌地圖的工作,我有標記的數組,它看起來像這樣:jquery css顏色正在改變每個選擇器,但字體大小不是?

var locations = [ 
     [{ 
      id: 1, 
      lat: 51.5239935252832, 
      lng: 5.137663903579778, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 2, 
      lat: 51.523853342911906, 
      lng: 5.1377765563584035, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 3, 
      lat: 51.5237298485607, 
      lng: 5.137969675407476, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 4, 
      lat: 51.52355628836575, 
      lng: 5.138066234932012, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 5, 
      lat: 51.52340275379578, 
      lng: 5.138211074218816, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 6, 
      lat: 51.523199152806626, 
      lng: 5.138382735595769, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 7, 
      lat: 51.5229955509073, 
      lng: 5.138511481628484, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 8, 
      lat: 51.52280529912936, 
      lng: 5.138543668136663, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 9, 
      lat: 51.523596340777075, 
      lng: 5.138463201866216, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 700, 
      lat: 51.523372714362736, 
      lng: 5.1386992362595265, 
      content: 'Kids Jungalow (5p)' 
     }], 
     [{ 
      id: 101, 
      lat: 51.52329594683302, 
      lng: 5.138838711128301, 
      content: 'Kids Jungalow Giraffe' 
     }] 

比我環槽標記列陣和一個HTML標記組這樣的每一個位置:

for (i = 0; i < locations.length; i++) { 
    var myLatLng = new google.maps.LatLng({ 
     lat: locations[i][0].lat, 
     lng: locations[i][0].lng 
    }); 
    var number = locations[i][0].id; 
    var marker_html = '<div id="' + number + '"><div class="rich-marker"><span class="number-id">' + number + '</span></div></div>'; 

    var marker = new RichMarker({ 
     position: myLatLng, 
     map: map, 
     flat: true, 
     anchor: RichMarkerPosition.MIDDLE, 
     content: marker_html 
    }); 
} 

比我想改變類number-id的CSS樣式縮放級別18這樣的:

map.addListener('zoom_changed', function() { 
     // Limit the zoom level 
     if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel); 

     // for loop door locaties 
     for (i = 0; i < locations.length; i++) { 
      setMarkerSize(i); 
     } 
    }); 

    function setMarkerSize(i) { 
     var infobox = locations[i][0].infobox; 
     var id = locations[i][0].id; 

     if (map.getZoom() === 18) { 
      $('.rich-marker').css({ 'display' : 'block', 'width' : '20px', 'height' : '20px' }); 
      $('.number-id').css({ 'display' : 'block', 'font-size' : '12px'}); 

      if (id >= 100) { 
       console.log(id); 
       console.log($('[data-id="' + id + '"]')); 

       $('#' + id).find('.number-id').css('font-size', '10px'); 
       $('#' + id).find('.number-id').css('color', 'red'); 
      } 

      infobox.setOptions({ pixelOffset: new google.maps.Size(-93, -22) }); 
     } 

    } 

的顏色是改變所有100以上,但字體大小隻改變爲數組中的最後一個對象爲什麼?

+0

'如果(ID> = 100){'這裏其中是的'id'從未來值? – dreamweiver

+0

@dreamweiver抱歉忘了提及一個 – Sreinieren

+0

因爲id不是在循環中構造的,所以它的值在if內部是常量,所以選擇器$('#'+ id).find('。number-id')'會只有唯一的值,因爲使用了id選擇器,這意味着css將無法應用於具有id> = 100的所有元素。 – dreamweiver

回答

2

每次在循環處理標記時要設置所有font-size屬性12px的:

$('.number-id').css({ 
    'display': 'block', 
    'font-size': '12px' 
    }); 

然後,設置當前標記爲「10px的」的font-size。這隻剩下最後一個你設置爲「10px」。

proof of concept fiddle

代碼片斷:

var geocoder; 
 
var map; 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 17, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var bounds = new google.maps.LatLngBounds(); 
 
    for (i = 0; i < locations.length; i++) { 
 
    var myLatLng = new google.maps.LatLng({ 
 
     lat: locations[i][0].lat, 
 
     lng: locations[i][0].lng 
 
    }); 
 
    bounds.extend(myLatLng); 
 
    var number = locations[i][0].id; 
 
    var marker_html = '<div id="' + number + '"><div class="rich-marker"><span class="number-id">' + number + '</span></div></div>'; 
 

 
    var marker = new RichMarker({ 
 
     position: myLatLng, 
 
     map: map, 
 
     flat: true, 
 
     anchor: RichMarkerPosition.MIDDLE, 
 
     content: marker_html 
 
    }); 
 
    } 
 
    map.setCenter(bounds.getCenter()); 
 
    var minZoomLevel = 18; 
 
    map.addListener('zoom_changed', function() { 
 
    document.getElementById('zoom').innerHTML = map.getZoom(); 
 

 
    // Limit the zoom level 
 
    if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel); 
 

 
    // for loop door locaties 
 
    for (i = 0; i < locations.length; i++) { 
 

 
     setMarkerSize(i); 
 
    } 
 
    }); 
 

 
    function setMarkerSize(i) { 
 
    var infobox = locations[i][0].infobox; 
 
    var id = locations[i][0].id; 
 

 
    if (map.getZoom() === 18) { 
 
     $('.rich-marker').css({ 
 
     'display': 'block', 
 
     'width': '20px', 
 
     'height': '20px' 
 
     }); 
 
     /* $('.number-id').css({ 
 
     'display': 'block', 
 
     'font-size': '12px' 
 
     }); */ 
 

 
     if (id >= 100) { 
 
     console.log(id); 
 
     console.log($('[data-id="' + id + '"]')); 
 

 
     $('#' + id).find('.number-id').css('font-size', '18px'); 
 
     $('#' + id).find('.number-id').css('color', 'red'); 
 
     } 
 

 
     /* infobox.setOptions({ 
 
     pixelOffset: new google.maps.Size(-93, -22) 
 
     }); */ 
 
    } 
 

 
    } 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize); 
 
var locations = [ 
 
    [{ 
 
    id: 1, 
 
    lat: 51.5239935252832, 
 
    lng: 5.137663903579778, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 2, 
 
    lat: 51.523853342911906, 
 
    lng: 5.1377765563584035, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 3, 
 
    lat: 51.5237298485607, 
 
    lng: 5.137969675407476, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 4, 
 
    lat: 51.52355628836575, 
 
    lng: 5.138066234932012, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 5, 
 
    lat: 51.52340275379578, 
 
    lng: 5.138211074218816, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 6, 
 
    lat: 51.523199152806626, 
 
    lng: 5.138382735595769, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 7, 
 
    lat: 51.5229955509073, 
 
    lng: 5.138511481628484, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 8, 
 
    lat: 51.52280529912936, 
 
    lng: 5.138543668136663, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 9, 
 
    lat: 51.523596340777075, 
 
    lng: 5.138463201866216, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 700, 
 
    lat: 51.523372714362736, 
 
    lng: 5.1386992362595265, 
 
    content: 'Kids Jungalow (5p)' 
 
    }], 
 
    [{ 
 
    id: 101, 
 
    lat: 51.52329594683302, 
 
    lng: 5.138838711128301, 
 
    content: 'Kids Jungalow Giraffe' 
 
    }] 
 
];
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&ext=.js"></script> 
 
<script src="https://cdn.rawgit.com/googlemaps/js-rich-marker/gh-pages/src/richmarker.js"></script> 
 
<div id="zoom"></div> 
 
<div id="map_canvas"></div>

相關問題