动态高度 jQuery

发布于 2024-12-18 14:57:56 字数 531 浏览 0 评论 0原文

我正在使用 Google 地图 API 来拉回带有一系列点的地图。我还拉回了 ID="directionsPanel" 元素中显示的方向。这一切都很好,除了当我们超过一定数量的点(Firefox 中为 3,Chrome 中为 4)时,它们会溢出页面。

我使用以下 jQuery 代码来检测元素高度是多少(大概是在加载方向之后),然后进行相应调整:

$(document).ready(function() {
  if($.browser.mozilla)
  {
    var sHeight = $("#directionsPanel").height();
    sHeight = (sHeight * 1.5);
    $("#directionsPanel").height(sHeight);
  }
  else
  {
    // do stuff for other browsers
  }

});

不幸的是,元素没有调整到正确的高度(也许它没有检测到正确的初始高度? )并且事情仍然从页面上溢出。关于如何使其更加稳健有什么想法吗?

I am using the Google Maps API to pull back a map with a series of points on it. I am also pulling back directions which are displayed in the element with ID="directionsPanel". This is all well and good except for the fact that when we get past a certain number of points (3 in Firefox, 4 in Chrome) they spill off the page.

I am using the following jQuery code to detect what the element height is (presumably after the directions are loaded) and then adjust it accordingly:

$(document).ready(function() {
  if($.browser.mozilla)
  {
    var sHeight = $("#directionsPanel").height();
    sHeight = (sHeight * 1.5);
    $("#directionsPanel").height(sHeight);
  }
  else
  {
    // do stuff for other browsers
  }

});

Unfortunately the element is not being adjusted to the right amount (perhaps it is not detecting the correct initial height?) and things are still spilling off the page. Any thoughts on how I might make this more robust?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-12-25 14:57:56

Google Maps API 可能允许您指定地图加载时的回调函数,这就是您应该放置代码的位置。

例如:

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var haight = new google.maps.LatLng(37.7699298, -122.4469157);
var oceanBeach = new google.maps.LatLng(37.7683909618184, -122.51089453697205);

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var myOptions = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: haight
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsDisplay.setMap(map);
}

function calcRoute() {
  var selectedMode = document.getElementById("mode").value;
  var request = {
      origin: haight,
      destination: oceanBeach,
      // Note that Javascript allows us to access the constant
      // using square brackets and a string value as its
      // "property."
      travelMode: google.maps.TravelMode[selectedMode]
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
    //YOUR CODE FOR RESIZING HERE
  });
}
<div>
<b>Mode of Travel: </b>
<select id="mode" onchange="calcRoute();">
  <option value="DRIVING">Driving</option>
  <option value="WALKING">Walking</option>
  <option value="BICYCLING">Bicycling</option>
</select>
</div>

不要使用 $(document).ready() 因为它会在 DOM 加载时立即执行。

Google Maps API probably lets you specify a callback function for when the map has loaded, and that is where you should put your code.

For example:

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var haight = new google.maps.LatLng(37.7699298, -122.4469157);
var oceanBeach = new google.maps.LatLng(37.7683909618184, -122.51089453697205);

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var myOptions = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: haight
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  directionsDisplay.setMap(map);
}

function calcRoute() {
  var selectedMode = document.getElementById("mode").value;
  var request = {
      origin: haight,
      destination: oceanBeach,
      // Note that Javascript allows us to access the constant
      // using square brackets and a string value as its
      // "property."
      travelMode: google.maps.TravelMode[selectedMode]
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
    //YOUR CODE FOR RESIZING HERE
  });
}
<div>
<b>Mode of Travel: </b>
<select id="mode" onchange="calcRoute();">
  <option value="DRIVING">Driving</option>
  <option value="WALKING">Walking</option>
  <option value="BICYCLING">Bicycling</option>
</select>
</div>

Don't use $(document).ready() since that executes immediately when the DOM is loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文