javascript - 分散的无序列表

标签 javascript jquery html-lists scatter screen-positioning

我有一个无序列表,我希望添加这样的效果:当向其中添加某些内容时,它每次都会出现在屏幕上的随机位置。但是,由于它是列表中的文本,因此我不能让每个随机位置重叠。附加的文本将有一个 5 秒的淡出计时器,因此一旦消息淡出,该空间将变得可用。

这可能吗?如果是的话,这里是我正在使用的 HTML:

<ul id="messagebox" >

</ul>

<div>

<input type="text" id="typetextbox" maxlength="120"  autocomplete="off" />

<button type="submit" id="submit" onblur="submit"> </button>


</div> 

这是 Javascript/jquery:

$(document).ready(function(){

  $('#typetextbox').keypress(function (e){
     if(e.keyCode == 13 ) $('#submit').click();
  });

  $('#submit').click(function(){
     var message = $('#typetextbox').val();
     if (message.replace(/ /g, ''))
       $('#messagebox').append(message + "<br/>");
     $("#typetextbox").val("");
  });
});

如果没有,我该怎么做才能达到这种效果?

谢谢大家!

最佳答案

http://jsfiddle.net/4a7Tj/2/

为了让列表项显示在随机位置,CSS 应该设置为 position:absolute 然后根据生成的随机值设置 left 和 top

CSS

li{
    height: 20px;
    background: orange;
    width:200px;
    margin:2px;
    padding:5px;
    position: absolute;
}

ul{
    list-style:none;
}

JavaScript

$(document).ready(function(){

  $('#typetextbox').keypress(function (e){
     if(e.keyCode == 13 ) $('#submit').click();
  });

  $('#submit').click(function(){
      var message = $('#typetextbox').val();
      if (message.replace(/ /g, '')){
          var positions = makeNewPosition();
          var el = $('<li>'+message+'</li>');
          el.attr('gridpos', positions[0].toString()+"x"+positions[1].toString())
          el.css('left', positions[1] * window.li_width);
          el.css('top', positions[0] * window.li_height);
          $('#messagebox').append(el);


          setTimeout(function() {
              el.fadeOut();
              var gridpos = el.attr('gridpos');
              delete window.grid[gridpos];
          }, 5000 );


      }
      $("#typetextbox").val("");
  });
});
window.grid = {};
window.li_height = 20;
window.li_width = 200;
function makeNewPosition(){

    // Get viewport dimensions (remove the dimension of the div)
    var h = Math.floor($(window).height()/window.li_height);
    var w = Math.floor($(window).width()/window.li_width);

    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);
    var gridpos = nh.toString() + "x" + nw.toString();
    if(typeof window.grid[gridpos] == 'undefined'){
        return [nh,nw];
    }else{
        //this could eventually run out of room, so make sure to clear the grid with delete window.grid[gridpos]; when it disappears from the screen.
        return makeNewPosition();
    }

}

网格位置基于窗口的高度和宽度以及列表项声明的高度宽度。

关于javascript - 分散的无序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149767/

相关文章:

javascript - 触发卸载导致错误

javascript - 在无序列表的行之间添加一个 div

javascript - 如何使用来自 silverlight 项目的多个参数调用 javascript 函数?

javascript - 未找到公共(public)目录 (404) ExpressJS 和 Jade

javascript - 使用 jquery 为每个输入值动态创建 JSON

javascript - 如何在圆形路径上呈现百分比数据

javascript - HTML 有序列表不正确显示子项

Javascript:无法将 href 添加到列表项

javascript - 为什么在 IE 中进行 setInterval div 替换后,我的容器会丢失其内容?

javascript - 为什么我的 Redux store 应该是可序列化的?