简体   繁体   English

获取多个元素的位置

[英]Get the positions of multiple elements

I am trying to get the individual positions of multiple draggable elements using jQuerys .position and .each functions. 我正在尝试使用jQuerys .position和.each函数来获取多个可拖动元素的单独位置。

This is what i have so far... 这是我到目前为止所拥有的...

JS JS

function newAppDraggable() {
    $('.App').draggable({
        containment: "#AppBox",
        grid: [ 60, 60 ],
        stack: ".App"
    });
};

$(function() {
    $('.AppList').droppable({
        accept: ".App",
        tolerance: 'fit',
        drop: function(event, ui) {
            $(".App").each(function( index ) {
            var position = $(this).position();
            var posTop = position.top;
            var posLeft = position.left;
            $( "#posThis" ).html(index+":"+posTop+":"+posLeft);
            });
        }
    });
});

HTML 的HTML

<div class="AppList">
    <div id="TimenDate" class="App Fixed Size110x350">
    </div>

    <div id="User" class="App Fixed Size110x290">
    <p id="posThis"></p>
    </div>

    <div id="Weather" class="App Fixed Size110x350">
    </div>
</div>

Here is a fiddle for better explanation. 这是一个小提琴,用于更好的解释。 On the fiddle the index stays at 2 and the positions only change when the 3rd app is moved. 在小提琴上,索引保持在2且位置仅在移动第三个应用程序时才会更改。

what I would like is to be able to get the positions of all the individual apps and possibly save them in an array in a cookie (or something of the sort) so that i can reload them in their last positions when the page is refreshed. 我想要的是能够获取所有单个应用程序的位置,并可能将它们保存在cookie(或某种形式)的数组中,以便在刷新页面时可以将它们重新加载到它们的最后位置。

In vanilla javascript and on modern browsers that support element.getBoundingClientRect 在香草javascript和支持element.getBoundingClientRect的现代浏览器中

Both of the following examples have been modified to demonstrate how the attained positions of each element can be added to an array that can then be accessed later. 修改了以下两个示例,以演示如何将每个元素的到达位置添加到一个数组中,然后可以在以后访问该数组。

You could get the position information with this tecnique. 您可以使用此技术获取位置信息。

var apps = document.querySelectorAll(".App"),
    positions = [];

Array.prototype.forEach.call(apps, function (app, index) {
    var positionInfo = app.getBoundingClientRect();

    positions.push(positionInfo);
    console.log(index + ":" + positionInfo.top + ":" + positionInfo.left);
});

console.log(positions);

On jsfiddle jsfiddle上

In the above example the broswer must also have support for Array.forEach and document.querySelectorAll 在上面的示例中,浏览器还必须支持Array.forEachdocument.querySelectorAll

Using jquery, it would be something like this, using jQuery selectors , jQuery.each and jQuery.position 使用jquery,使用jQuery选择器jQuery.eachjQuery.position就像这样。

var apps = $(".App"),
    positions = [];

$.each(apps, function (index, app) {
    var positionInfo = $(app).position();

    positions.push(positionInfo);
    console.log(index + ":" + positionInfo.top + ":" + positionInfo.left);
});

console.log(positions);

on jsfiddle jsfiddle上

About JavaScript 关于JavaScript

JavaScript® (often shortened to JS) is a lightweight, interpreted, object-oriented language with first-class functions, most known as the scripting language for Web pages, but used in many non-browser environments as well such as node.js or Apache CouchDB. JavaScript®(通常缩写为JS)是一种轻量级,解释性,面向对象的语言,具有一流的功能,通常被称为网页的脚本语言,但在许多非浏览器环境以及node.js或Apache CouchDB。

The JavaScript standard is ECMAScript. JavaScript标准是ECMAScript。 As of 2012, all modern browsers fully support ECMAScript 5.1. 截至2012年,所有现代浏览器均完全支持ECMAScript 5.1。 Older browsers support at least ECMAScript 3. A 6th major revision of the standard is in the works. 较旧的浏览器至少支持ECMAScript3。该标准的第6个主要修订版正在开发中。 The current progress of different new and improved features can be followed on the dedicated wiki. 专用Wiki上可以跟踪各种新功能和改进功能的最新进展。

This section of the site is dedicated to the JavaScript language itself, the parts that are not specific to Web pages, or other host environments. 该站点的这一部分专门针对JavaScript语言本身,不特定于网页或其他宿主环境的部分。 For information about APIs specific to Web pages, please see DOM. 有关特定于网页的API的信息,请参阅DOM。 Read more about how DOM and JavaScript fit together in the DOM Reference. 在DOM Reference中阅读有关DOM和JavaScript如何组合在一起的更多信息。

About jQuery 关于jQuery

jQuery is a fast, small, and feature-rich JavaScript library. jQuery是一个快速,小型且功能丰富的JavaScript库。 It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. 借助易于使用的API(可在多种浏览器中使用),使HTML文档的遍历和操作,事件处理,动画和Ajax等事情变得更加简单。 With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript. 兼具多功能性和可扩展性,jQuery改变了数百万人编写JavaScript的方式。

About console.log 关于console.log

Summary 摘要

Outputs a message to the Web Console. 将消息输出到Web控制台。

About Zero-based numbering 关于从零开始的编号

Zero-based numbering is numbering in which the initial element of a sequence is assigned the index 0, rather than the index 1 as is typical in everyday circumstances. 从零开始的编号是一种编号,其中为序列的初始元素分配索引0,而不是通常情况下的索引1。

Javascript Array Javascript 数组

Arrays are list-like objects that come with a several built-in methods to perform traversal and mutation operations. 数组是类列表对象,带有几种内置方法来执行遍历和变异操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM