简体   繁体   English

jQuery的定位元素在Internet Explorer 8中不起作用

[英]Positioning element with jQuery does not work in Internet Explorer 8

I have a set of images inside a div that I position with jQuery on page load. 我在div上放置了一组图片,这些图片在页面加载时与jQuery一起定位。

var myimg = $('#myimg');
... // Calculate x and y
myimg.css('left', x);
myimg.css('bottom', y);

Each image has a position: absolute style, while their containing div has a position: relative style. 每个图像都有一个position: absolute样式,而其包含的div有一个position: relative样式。

This works in all browsers except for Internet Explorer 8 (have not tried 9). 该功能在除Internet Explorer 8(尚未尝试9)之外的所有浏览器中均有效。 The images do not get their positions set. 图像未设置位置。 I can confirm that the JavaScript is running, especially since I'm also rotating the images using jqueryrotate to rotate the images when I position them, and they appear rotated in IE. 我可以确认JavaScript正在运行,尤其是因为我还在定位时使用jqueryrotate旋转图像来旋转图像,并且它们在IE中显示为旋转。

Edit 编辑

This is the full JavaScript code. 这是完整的JavaScript代码。 What I'm trying to do specifically is arrange the images in a circle. 我要专门做的是将图像排列成一个圆圈。

var num_imgs = 14;

var angle_delta = 360 / num_imgs;

var center_x = 365;
var center_y = 245;
var radius = 230;

for (var i = 0; i < num_imgs; ++i) {
    var angle = (angle_delta * i) + 90;

    var radians = angle * 0.0174532925;
    var x = Math.round(radius * Math.cos(radians));
    var y = Math.round(radius * Math.sin(radians));

    var img = $('#myimg-' + i);
    img.rotate(90 - angle); // Only part of the code that works
    img.css('left', center_x + x);
    img.css('bottom', center_y + y);
}

Are your x and y values simple integers, or do they have the 'px' suffix? 您的x和y值是简单的整数,还是后缀为'px'? They should be something like '100px', not simply 100, so if you are missing the px, that could be the problem. 它们应该类似于“ 100px”,而不仅仅是100,因此,如果您缺少px,则可能是问题所在。

Try wrapping each image in a <div> and assign your myimg# id to that div instead of directly to the image. 尝试将每个图像包装在<div>然后将myimg# id分配给该div,而不是直接分配给该图像。 Add a class to each image-containing <div> so that you can style them with absolute positioning all at once. 向每个包含图像的<div>添加一个类,以便您可以一次对它们进行绝对定位。

You'll need to use a little DOM traversal to get to the actual image, but that's only important for the plugin -- just use img.children('img').rotate(90 - angle) instead of img.rotate(90 - angle) . 您需要使用一些DOM遍历才能获得实际的图像,但这仅对插件很重要-只需使用img.children('img').rotate(90 - angle)而不是img.rotate(90 - angle) Your CSS positioning will all use the <div> . 您的CSS定位将全部使用<div>

http://jsfiddle.net/R86Ud/4/ http://jsfiddle.net/R86Ud/4/

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

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