jquery - 在定义的空间内分散图像,这样它们就不会重叠

发布于 2025-01-01 18:10:21 字数 1303 浏览 4 评论 0原文

各位高素质编码员和其他人大家好!

我正在使用 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);
    }
);

我的脚本编写技能对此还不够,所以任何帮助将不胜感激。

谢谢!

Hello to all coders of quality and others!

I am using a jquery script (Author: Marco Kuiper - www.marcofolio.net) that randomly places images within a defined space. Using a mouse the images can be dragged around on the screen, then clicked to enlarge.

The problem is when the page is viewed on an iphone, the images cannot be dragged so if any images are overlapping they cannot be easily tapped and enlarged. My idea is to create an array of random coordinates that are at least the width of each image away from each other while still fitting with the confines of the enclosing div. The images are all the same dimensions. Once that array is created, use it's values to position the images.

Here's the current script that places the images:

        $(".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);
    }
);

My scripting skills are not quite sufficient for this, so any help would be appreciated.

Thanks!

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

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

发布评论

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

评论(1

彼岸花ソ最美的依靠 2025-01-08 18:10:21

我需要查看您的 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);
    }
  );

I'd need to see your HTML and CSS to help with the grid layout, but as far as the javascript goes, and if random rotation will suffice, you can just remove the bits about positioning:

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