向Javascript中使用的图像添加css样式
我在我的js文件中使用了一个图像(它用于地理位置图标),我想添加css样式,因为图像本身在屏幕上太大了向Javascript中使用的图像添加css样式,javascript,css,Javascript,Css,我在我的js文件中使用了一个图像(它用于地理位置图标),我想添加css样式,因为图像本身在屏幕上太大了 <!DOCTYPE html> <html> <head> <style> html, body { height: 100%; margin: 0; padding: 0; overflow: hidden;
<!DOCTYPE html>
<html>
<head>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var locationIcon = 'img/SVG/User_Location.svg';
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
//Centers map to current position
map.setCenter(pos);
//Sets icon to current position
var currentLocation = new google.maps.Marker({
position: {lat: position.coords.latitude, lng: position.coords.longitude},
map: map,
icon: locationIcon
});
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
</script>
</body>
</html>
html,正文{
身高:100%;
保证金:0;
填充:0;
溢出:隐藏;
}
#地图{
身高:100%;
}
var locationIcon='img/SVG/User_Location.SVG';
//试试HTML5地理定位。
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
var pos={
纬度:位置坐标纬度,
lng:position.coords.longitude
};
//中心映射到当前位置
地图设置中心(pos);
//将图标设置为当前位置
var currentLocation=new google.maps.Marker({
位置:{lat:position.coords.latitude,lng:position.coords.longitude},
地图:地图,
图标:位置图标
});
},函数(){
handleLocationError(true,infoWindow,map.getCenter());
});
}否则{
//浏览器不支持地理位置
handleLocationError(false,infoWindow,map.getCenter());
}
}
我将如何实现这一点?编辑:我添加了代码来显示图像的显示方式我想这篇文章回答了您的问题。 您希望您的图像位于.js文件之外。您可以在链接解决方案的函数中添加一个类,该函数将使用元素创建该类。从那里,您可以通过以下方式选择类:
.custom-class {
width: 100%;
height: 100%;
etc...
}
请向我们展示用于显示过大图像的JS代码?你想要实现什么,最终结果应该是什么?我认为SVG不能太大,它们是可伸缩的。为什么要使用javascript进行样式设置?为什么不只用CSS呢?你可以在创建标记时使用“缩放”来设置大小。