【问题标题】:random position of images图像的随机位置
【发布时间】:2015-10-28 20:51:28
【问题描述】:

我找到了一个可以随机定位 div/图像的脚本。但是,它并没有完全按照我想要/需要的方式工作。

图像在一个 div 中加载(我猜这并不理想)。我有大约 30 张图片。

但是它们不能很好地加载,var posy = (Math.random() * ($(document).height() - 0)).toFixed(); 也不能很好地工作。图片大多加载在顶部(我认为博客中的图片不算数,所以它在没有图片的情况下获得高度?)

所以我想要的是:更好地加载它们 随机化它们,使它们也到达页面底部

var circlePosition = document.getElementsByClassName('circle');
console.log(circlePosition);

function position() {
  for (var i = 0; i < circlePosition.length; i++ ) {
    //give circle a random position
    var posx = (Math.random() * ($(document).width() - 0)).toFixed();
    var posy = (Math.random() * ($(document).height() - 0)).toFixed();



    //apply position to circle
    $(circlePosition[i]).css({
      'position':'absolute',
      'left':posx+'px',
      'top':posy+'px',
    })
  } 
} //end function position

var circleTotal = circlePosition.length;

$('.circle').click(function() {
  $(this).fadeOut();
  circleTotal = circleTotal - 1;
  console.log(circleTotal);

  if(circleTotal == 0) {
    position()
    $('.circle').fadeIn();
  }

});

position();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img src="//placehold.it/1x5000"> <!-- Placeholder image to show issue -->
<div class="circle">
  <img src="http://static.tumblr.com/tensqk8/k8anq0438/01.png">
</div>
<div class="circle">
  <img src="http://static.tumblr.com/tensqk8/k8anq0438/01.png">
</div>

【问题讨论】:

  • 它实际上对我来说还不错:看起来相当随机,我没有看到页面顶部附近分布不均匀的图像。但我将发布与 MCM 相同的评论:尝试$(window).load(position);,因为加载事件会在所有图像加载后触发,如果图像没有定义高度,则文档高度可能会改变。您可能会得到非常快的响应。在您当前对position(); 的调用中设置断点并检查$(document).height() 的值。我的大约 15,000 像素左右。另外:不要做随机图像。除非您为内容添加碰撞检测,否则用户体验很差。
  • 我已经用一个非常高的图像编辑了您的 sn-p 以显示问题。 @MCM 的回答是正确的,它应该在 window.onload$(window).load() 事件监听器中。另外,您应该注意到,这只发生在您第一次加载页面时,加载图像后,结果将被缓存。

标签: javascript image random


【解决方案1】:

一个干净易读且不依赖 jQuery 的解决方案可能是这样的。它通过定位图像本身来避免不必要地将图像包装在 div 中。它包含一个隐藏元素,类似于“穷人”shadow DOM

http://jsfiddle.net/sean9999/yv9otwr7/9/

;(function(window,document,undefined){
    "use strict";
        var init = function(){    
            var canvas = document.querySelector('#x');
            var icon_template = document.querySelector('#template');
            var icon_width = 40;
            var icon_height = 30;
            var the_images = [
                'http://static.tumblr.com/tensqk8/k8anq0438/01.png',
                'http://static.tumblr.com/tensqk8/rYanq05el/04.png',
                'http://static.tumblr.com/tensqk8/SYknq05py/05.png',
                'http://static.tumblr.com/tensqk8/s7inq057d/03.png'
            ];
            var pickRandomImage = function(){
                var i = Math.floor( Math.random() * the_images.length );
                return the_images[i];
            };
            var total_number_of_images = 10;
            var max_height = canvas.offsetHeight - icon_height;
            var max_width = canvas.offsetWidth - icon_width;
            var randomCoordinate = function(){
                var r = [];
                var x = Math.floor( Math.random() * max_width );
                var y = Math.floor( Math.random() * max_height );
                r = [x,y];
                return r;
            };
            var createImage = function(){
                var node = icon_template.cloneNode(true);
                var xy = randomCoordinate();
                node.removeAttribute('id');
                node.removeAttribute('hidden');
                node.style.top = xy[1] + 'px';
                node.style.left = xy[0] + 'px';
                node.setAttribute('src',pickRandomImage());
                canvas.appendChild(node);
            };
            for (var i=0;i<total_number_of_images;i++){
                createImage();
            };
        };
       window.addEventListener('load',init);
})(window,document);
body {
    background-color: #fed;
}

#x {
    border: 3px solid gray;
    background-color: white;
    height: 400px;
    position: relative;
}

#x .icon {
    position: absolute;
    z-index: 2;
}
<h1>Randomly distributed images</h1>

<div id="x"></div>

<img src="#" class="icon" hidden="hidden" id="template" />

【讨论】:

  • cool :) 如果您认为它正确回答了您的问题,请标记为这样
【解决方案2】:

尝试移动你的位置();在 $(window).load 函数内部调用。

我认为可能是在所有图像加载之前定位图像,因此页面会更短。

【讨论】:

  • 你能把它当作答案而不是猜测吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多