首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jquery动态定位

jquery动态定位
EN

Stack Overflow用户
提问于 2013-03-25 22:59:46
回答 2查看 290关注 0票数 0

我有一个嵌套在div中的动态文本区域。div是可调整大小和可拖动的。我已经为div创建了一个删除图标,但希望删除图标位于div的NE角,并在div调整大小时移动。

我已经用下面的代码创建了div、textarea和图标

代码语言:javascript
复制
    function NewTextArea(id)
    {   

    id=id+i;
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', id);
    newdiv.setAttribute('class', 'dragbox');
    newdiv.setAttribute('iterate',i);
    newdiv.style.position = "relative";
    newdiv.style.top = p;
    newdiv.style.left = p;
    newdiv.style.cursor='move';
    newdiv.innerHTML = "</div><br><textarea id='"+i +"'  onDblClick='editor1("+i+")' name='textarea["+i +"]' class='textarea1' style='position:absolute; top:10px;left:0px;overflow-y: auto;background-color:transparent;border: 2px dashed #000; '>some text here"+i+"</textarea>";
    newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='"+i+"' name='id["+i+"]'><br><input name='box_type["+i+"]' type='hidden' value='text'/>"; 
    newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='300' name='width["+i+"]' id='width"+i+"'><br><input type='hidden' value='300' name='height["+i+"]' id='height"+i+"'>";               
    newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='0' name='left["+i+"]' id='left"+i+"'><br><input type='hidden' value='0' name='top["+i+"]' id='top"+i+"'>"; 

    document.getElementById("frmMain").appendChild(newdiv); 

    var but=document.createElement('input');
    but.setAttribute('type','button');
    but.setAttribute('class','removebutton');       

    but.onclick=function(){
        if(confirm('Really delete?'))
        {
        document.getElementById("frmMain").removeChild(newdiv);
        document.getElementById(id).removeChild(but);
        document.getElementById(id).removeChild(newbr);
        }       
    }

    newbr=document.createElement('BR');
    document.getElementById(id).appendChild(newbr);
    document.getElementById(id).appendChild(but);



    $(function()
    {
        $("#"+i).resizable({autoHide:true})
        $("#"+id).hover(function () {
            $("#"+i).css('border', '2px dashed #000');         
        });
        $("#"+id).mouseleave(function () {
            $("#"+i).css('border', '0px dashed #000');         
        });
        $("#"+i).resizable(
            {
                stop: function(event, ui)
                {
                    var width = ui.size.width;
                    var height = ui.size.height;
                    // alert("width="+width+"height="+height);
                    ValProportions(width,height,ui.element.context.id);           
                }
            });

        $( "#"+id ).draggable(
            {
                stop: function(event, ui)
                {
                    Stoppos = $(this).position();
                    $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
                    // alert("left="+Stoppos.left+"top="+Stoppos.top);
                    ValPostion(Stoppos.left,Stoppos.top,$(this).attr('iterate'));   
                }
            }); 
        $("#"+i).draggable({handle:"#handle"}); 
    });  

    function ValProportions(defaultwidth, defaultheight,id)  { 
        $('#width'+id).val(defaultwidth);
        $('#height'+id).val(defaultheight);
    }

   function ValPostion(defaultleft,defaulttop,id)  {  
        $('#left'+id).val(defaultleft);
        $('#top'+id).val(defaulttop);
    }

    p=p+25;     
    i++;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-25 23:29:26

在按钮上使用样式。

代码语言:javascript
复制
float:right

会把你的按钮放在div的右边。只需确保您将按钮作为div中的第一个元素。

票数 1
EN

Stack Overflow用户

发布于 2013-03-25 23:02:47

添加样式

代码语言:javascript
复制
.dragbox {
    position: relative;
}

.removebutton {
    position: absolute;
    top: 5px;
    right: 5px;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15618093

复制
相关文章

相似问题

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