【问题标题】:jquery - scattering images within a defined space so they don't overlapjquery - 在定义的空间内分散图像,因此它们不会重叠
【发布时间】:2012-02-24 17:46:29
【问题描述】:

您好所有质量编码人员和其他人!

我正在使用 jquery 脚本(作者:Marco Kuiper - www.marcofolio.net)将图像随机放置在定义的空间内。使用鼠标可以在屏幕上拖动图像,然后单击放大。

问题是在 iphone 上查看页面时,无法拖动图像,因此如果任何图像重叠,则无法轻松点击和放大。我的想法是创建一个随机坐标数组,这些坐标至少与每个图像的宽度相同,同时仍然符合封闭 div 的范围。图像都是相同的尺寸。创建该数组后,使用它的值来定位图像。

这是放置图像的当前脚本:

        $(".polaroid").each
(function ()
    {
    var tempVal = Math.round(Math.random());
    if(tempVal == 1)
        { var rotDegrees = randomXToY(330, 360); } // rotate left
    else
        { var rotDegrees = randomXToY(0, 30); } // rotate right

    var position = $(this).parent().offset();
    var wiw = $(this).parent().width(); // width of div enclosing object
    var wih = $(this).parent().height(); // heiht of div enclosing object

    var leftpos = Math.random()*(wiw - $(this).width()) + position.left;
    var toppos = Math.random()*(wih - $(this).height()) + position.top;

    var cssObj =
        {
        'left' : leftpos,
        'top' : toppos,
        '-webkit-transform' : 'rotate('+ rotDegrees +'deg)',  // safari only
        '-moz-transform' : 'rotate('+ rotDegrees +'deg)',  // firefox only
        'tranform' : 'rotate('+ rotDegrees +'deg)' // if CSS3 is standard
        };
    $(this).css(cssObj);
    }
);

我的脚本技能不足以完成此任务,因此我们将不胜感激。

谢谢!

【问题讨论】:

  • 是否有原因图片不能在移动设备的网格中布置,而是随机散布给其他人?
  • 当然,我可以在移动屏幕上使用网格中的图像。不过,我想保持轻微的随机旋转。

标签: jquery image random position overlapping


【解决方案1】:

我需要查看您的 HTML 和 CSS 以帮助进行网格布局,但就 javascript 而言,如果随机旋转就足够了,您可以删除有关定位的部分:

$(".polaroid").each
  (function ()
    {
    var tempVal = Math.round(Math.random());
    if(tempVal == 1)
        { var rotDegrees = randomXToY(330, 360); } // rotate left
    else
        { var rotDegrees = randomXToY(0, 30); } // rotate right

    var cssObj =
        {
        '-webkit-transform' : 'rotate('+ rotDegrees +'deg)',  // safari only
        '-moz-transform' : 'rotate('+ rotDegrees +'deg)',  // firefox only
        'tranform' : 'rotate('+ rotDegrees +'deg)' // if CSS3 is standard
        };
    $(this).css(cssObj);
    }
  );

【讨论】:

  • 谢谢,我希望事情就这么简单。如果 HTML 包含
      ,它可能会起作用。但很可惜,事实并非如此。这是 HTML:
  • 和 CSS:#PolaroidWrapper { width:700px;边距:0 自动; } #polaroidcontainer { 宽度:700px;高度:600px; } .polaroid { 宽度:147px;高度:150px;背景图像:url(画廊/图像/宝丽来-bg.png); // 宝丽来相框图片位置:绝对; -webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.6); -moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.6);盒子阴影:0 0 10px rgba(0, 0, 0, 0.6); } .polaroid img { 宽度:134px;高度:110px;边距:11px 0 0 7px; }
  • 啊,是的。每个图像都包含在一个 div 中,这是一个块级元素,因此您需要在 CSS 中浮动它们。检查这个:jsfiddle.net/4MJmf
  • 非常感谢,很抱歉我花了这么长时间才回复这个问题。您的解决方案效果很好!
猜你喜欢
  • 2015-12-24
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多