Positioning the element second time using JQuery U

2019-07-19 03:02发布

Here is my fiddle: http://jsfiddle.net/Ya3w7/2/

HTML:

<img src="http://cdn.tacky.me/m/static/settings16.png" class="settings-icon"/>

<div id="control-panel">
    <img src="http://cdn.tacky.me/m/static/settings16.png" />
    <a href="#" style="float:right" id="close-cp">X</a>
    <div class="link_container"><a href="#">Show Profile</a></div>
</div>

CSS:

.settings-icon 
{
    margin: 100px;
    cursor: pointer;
}

#control-panel
{
    position: absolute;
    height: auto;
    width: auto;
    top: 0;
    left: 0;
    background-color: #fff;
    font-family: Arial, sans-serif;
    display: none;
    z-index: 4;
}

JavaScript:

$('.settings-icon').click(function(){
    $('#control-panel').position({
        of: $('.settings-icon'),
        my: 'left top',
        at: 'left top'
    });
    $('#control-panel').show();
});

$('#close-cp').click(function(event){
    event.preventDefault();
    $('#control-panel').hide();
});

What I am trying to do: I have a settings image, upon clicking which it positions an absolutely positioned div (called contgrol-panel) around this img.

What I am seeing:

First time I click on the img it works fine

I dismiss the control panel by clicking on X on the top right corner

Second time I click the control panel shows up somewhere else

How to Repro

Go to the fiddle

  • Click on the settings icon for the first time
  • Dismiss the control panel which drops down by clicking on the X
  • Click on the settings icon once again
  • See control panel popping up somewhere else

2条回答
一夜七次
2楼-- · 2019-07-19 03:39

because jquery ui doesn't support position to hidden element, you have to show and position.

$('.settings-icon').click(function(){
  $('#control-panel').show().position({
    of: $(this),
    my: 'left top',
    at: 'left top'
   });
});
查看更多
你好瞎i
3楼-- · 2019-07-19 03:40

You just need to put the control panel back to 0, 0 after it's closed. Here's the updated fiddle http://jsfiddle.net/Ya3w7/3/.

$('.settings-icon').click(function(){
    $('#control-panel').position({
        of: $(this),
        my: 'left top',
        at: 'left top'
    });
    $('#control-panel').show();
});

$('#close-cp').click(function(event){
    event.preventDefault();
    $('#control-panel').css({top: '0px', left: '0px'}).hide();
});
查看更多
登录 后发表回答