首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将div元素放在同一位置,并在重新加载页面时检索.data()值

问如何将div元素放在同一位置,并在重新加载页面时检索.data()值
EN

Stack Overflow用户
提问于 2019-09-27 02:48:42
回答 1查看 36关注 0票数 0

我正在尝试将div的标签附加到div中,而且div的标签是可调整大小和可拖动的。它们还使用.data()存储一些值。演示如下所示

​

​

我需要将div放在父div中的相同位置,并获取我为每个div存储的值,即使我重新加载页面也是如此

用于在大的父div中附加div的代码

代码语言:javascript
复制
            width: wbox.val() * zoom,
            height: lbox.val() * zoom,
            padding: '0.5em',
            'font-size': '1em',
            border: '2px solid',
            'user-select': 'none',
            'background-color': 'gray',
            cursor: 'pointer',
            position: 'absolute',
            // 'font-size': '',
            block: 'inline'
        });
        $element.attr('id', component.val());
        $('#convas').append($element);
        $element.data('w', wbox.val());
        $element.data('l', lbox.val());

        $element.draggable({
            containment: '#convas',
        });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-02 16:30:21

简单的方法是创建每个div的属性列表,并将其存储在本地存储或其他地方,然后在用户重新加载页面时检索它。

存储

代码语言:javascript
复制
   function addDraggableBox(
    canvas_id = null,
    id,
    length = null,
    width = null,
    componentValue = null,
    classificationData = null,
    tile_position = null
) {
    let component_value = componentValue || component.children(':selected').text(); // text to be shown for a component inside canvas
    let bg_color = 'grey';
    let txt_color = 'black';
    const component_width = Number(width) || Number(wbox.val());
    const component_length = Number(length) || Number(lbox.val());

    component_value += ` (Area: ${component_width * component_length} sq.m)`; // add area to the component canvas text

    if (classificationData) {
        const { bgColor, txtColor } = classificationData;

        bg_color = bgColor;
        txt_color = txtColor || txt_color;
    }

    var $element = $('<div class="draggablebox" />').text(component_value).css({
        width: component_width * zoom,
        height: component_length * zoom,
        padding: '0.5em',
        'font-size': '1em',
        border: '2px solid',
        'user-select': 'none',
        background: bg_color,
        color: txt_color,
        cursor: 'pointer',
        position: 'absolute',
        block: 'inline',
        top: tile_position ? tile_position.top + 'px' : 0 || 0,
        left: tile_position ? tile_position.left + 'px' : 0 || 0
    });
    $element.attr('id', id);
    $(canvas_id || '#convas').append($element);
    $element.on('mouseup', draggableboxEvent);

    $element.draggable({
        containment: canvas_id || '#convas',
        drag: function(event, ui) {
            const reposition = ui.position;
            const tile_id = event.target.id;
            const floor_id = floor_dropdown.value;

            for (const floor of floor_data) {
                if (Number(floor.id) === Number(floor_id)) {
                    const tile = floor.tiles.find((t) => t.id === tile_id);

                    if (tile) {
                        tile['position'] = reposition;
                    }

                    re_draw(floor_id);
                    break;
                }
            }
        }
    });
    usersitearea += lbox.val() * wbox.val();

    var areacal = usersitearea * 100 / totalsitearea;
    $('#freespace').text(100 - areacal + '%');
} 

检索

代码语言:javascript
复制
   function re_draw(floorId = null) {
    const floor_id = floorId || floor_dropdown.value;
    const site_length = Number(localStorage.getItem('siteLenght')) || 800;
    const site_width = Number(localStorage.getItem('siteWidth')) || 800;
    const zoom = $('#zoomcanvas').val();

    if (is_basement) {
        floor_heading.text('Basement ' + floor_id);
    } else {
        floor_heading.text('Floor ' + (Number(floor_id) - Number(no_of_basements)));
    }

    const cur_floor_data = floor_data.find((floor) => Number(floor.id) === Number(floor_id));

    canvas.empty(); // empty the DOM

    // set height & width of canvas
    canvas.css({ height: site_length * zoom, width: site_width * zoom });

    if (cur_floor_data) {
        cur_floor_data.tiles.forEach((tile) => {
            const component_value = components_data.find((c) => Number(c.id) === Number(tile.componentId)).value;
            const classificationData = components_classification.find(
                (c) => Number(c.id) === Number(tile.classification)
            );
            const tile_position = tile.position || null;

            addDraggableBox(null, tile.id, tile.length, tile.width, component_value, 
        });
    }

    localStorage.setItem('floor_data', JSON.stringify(floor_data));
} ```

make sure you parse the list with JSON parser when you retrieve and JSON stringify when you set the value.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58123015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档