【发布时间】:2018-02-20 16:27:16
【问题描述】:
我有屏幕宽度、屏幕高度(例如 1360 X 768)和图像宽度和高度(例如 400 X 400)。我想在屏幕上的随机 x、y 位置显示每个图像,并在该位置显示图像。另外,随意旋转图像。
所以,我使用以下代码从屏幕中找到 X、Y 随机位置。
var randPosX = Math.floor((Math.random()*(screen_width - image_width )));
var randPosY = Math.floor((Math.random()*(screen_height - image_height )));
所以,这将在屏幕区域内返回 X、Y,以便显示整个图像。
现在,我已经使用轮换代码了
var rNum = Math.round((Math.random()*360)+1);
在 div 上应用
$('img').css('left',Math.abs(new_x_point) );
$('img').css('top', Math.abs(new_y_point));
$('img').css('position','absolute');
$('img').css('transform','rotate('+rNum+'deg)');
现在,旋转后,X、Y 位置发生了变化,因此有时它会剪切屏幕上的图像。
我还尝试在旋转后找到新的 X、Y 并使用以下逻辑应用它:
new_x_point = randPosX * Math.cos(rNum) - randPosY * Math.sin(rNum);
new_y_point = randPosY * Math.cos(rNum) + randPosX * Math.sin(rNum);
HTML
<div id="image_0" style="display:none;height:768px;width:1360px;z-index:1;position: absolute"><img src="001.png" style="height:400px;width:400px;" class="product-img"></div>
<div id="image_1" style="display:none;height:768px;width:1360px;z-index:1;position: absolute"><img src="002.png" style="height:400px;width:400px;" class="product-img"></div>
<div id="image_2" style="display:none;height:768px;width:1360px;z-index:1;position: absolute"><img src="003.png" style="height:400px;width:400px;" class="product-img"></div>
等
但它不起作用:(。请帮助谢谢
【问题讨论】:
-
你能把它做成 Codesn-p 以便我们看到你的整个 html 和 js 吗?
-
html 是简单的图像堆栈。
-
我想在旋转后显示图像,这样它就不会在屏幕上剪切。全图显示。
-
$('img').css() $('img').css() $('img').css() $('img').css()yurgh,伤了我的眼睛。您连续构建 4 个相同的 jQuery 对象,这浪费了很多处理能力。构建一次并传递一个包含所有 CSS 属性的对象:$('img').css({ left : Math.abs(new_x_point) , top : Math.abs(new_y_point) }) -
@JeremyThille:我会改变的。你也能解答一下问题的解决方法吗?
标签: javascript jquery html css