2016-01-24 86 views
1

我似乎無法弄清楚什麼jQueryUI可調整大小的功能是爲了造成我創建的「聊天框」div元素的錨點。問題是,當您通過拖動右上角來調整此元素的大小時,它會正確調整大小,但是當您按關閉按鈕播放jQuery動畫以將其摺疊時,它將向錯誤的方向摺疊。如果您根本沒有調整框的大小,那麼這個摺疊動畫可以正常工作。jQuery UI調整大小的錯誤與幻燈片動畫

似乎還有另一個問題,調整它的大小會導致框跳到頁面上的高點,但這似乎只發生在谷歌瀏覽器,Firefox工作正常,不知道爲什麼!

嘗試調整大小的方框,然後關閉它,看問題:

$(document).ready(function() { 
 
    // controls resizing of the chat box 
 
    $('.chat_box').resizable({ 
 
    handles: 'n, e, ne', 
 
    minWidth: 300, 
 
    minHeight: 100, 
 
    maxWidth: 700, 
 
    maxHeight: 500, 
 
    }); 
 
}); 
 

 
function minimize(chatId) { 
 
    var bottom_bar = document.getElementById("bottom_bar"); 
 
    var box = bottom_bar.getElementsByClassName("chat_box")[chatId]; 
 
    var bar = bottom_bar.getElementsByClassName("chat_bar")[chatId]; 
 
    bar.className = "chat_bar chat_box_minimized"; 
 

 
    $(box).stop().animate({ 
 
     height: "0px", 
 
     width: bar.offsetWidth, 
 
    }, 
 
    'normal', function() { 
 
     $(box).hide(); 
 
    } 
 
); 
 
}
#bottom_bar { 
 
    position: fixed; 
 
    z-index: 10; 
 
    bottom: 0px; 
 
    left: 0px; 
 
    right: 0px; 
 
    max-height: 40px; 
 
    background-color: #0042b3; 
 
    padding: 2px 20px; 
 
} 
 
div.chat_box { 
 
    position: fixed; 
 
    width: 350px; 
 
    height: 180px; 
 
    margin: 0px 4px; 
 
    bottom: 45px; 
 
} 
 
div.close_btn { 
 
    position: absolute; 
 
    top: 0px; 
 
    right: 0px; 
 
    width: 30px; 
 
    height: 100%; 
 
} 
 
div.close_btn:before { 
 
    content: 'x'; 
 
    display: block; 
 
    text-align: center; 
 
    vertical-align: middle; 
 
    line-height: 25px; 
 
    font-weight: bold; 
 
    font-family: Arial, sans-serif; 
 
    pointer-events: none; 
 
} 
 
div.close_btn:hover { 
 
    background-color: rgba(0, 9, 26, 0.8); 
 
    cursor: pointer; 
 
} 
 
div.chat_box_maximized { 
 
    background-color: white; 
 
    width: 350px; 
 
    margin: 5px 0px; 
 
    padding: 2px; 
 
    border: 3px solid #0045cc; 
 
    border-radius: 5px; 
 
    display: inline-block; 
 
} 
 
div.chat_box_maximized input { 
 
    width: 100%; 
 
    border: none; 
 
} 
 
div.chat_box_maximized p { 
 
    display: none; 
 
} 
 
div.chat_box_minimized { 
 
    background-color: #002266; 
 
    ; 
 
    max-width: 200px; 
 
    min-width: 80px; 
 
    margin: 5px 0px; 
 
    padding: 2px; 
 
    border: 3px solid #002266; 
 
    ; 
 
    border-radius: 5px; 
 
    display: inline-block; 
 
} 
 
div.chat_box_minimized:hover { 
 
    background-color: #3378ff; 
 
    border: 3px solid #3378ff; 
 
    cursor: pointer; 
 
} 
 
div.chat_box_minimized form { 
 
    display: none; 
 
} 
 
div.chat_box_minimized p { 
 
    margin: 0px 5px; 
 
    font-size: 10pt; 
 
    color: white; 
 
    font-weight: bold; 
 
    pointer-events: none; 
 
} 
 
.light_container, 
 
.dark_container { 
 
    -webkit-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57); 
 
    -moz-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57); 
 
    box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57); 
 
    border: 1px solid #005eff; 
 
    padding: 1px; 
 
} 
 
.light_container { 
 
    background-color: rgba(0, 34, 102, 0.9); 
 
} 
 
.dark_container { 
 
    background-color: rgba(0, 9, 26, 0.9); 
 
} 
 
.light_container .body, 
 
.dark_container .body { 
 
    padding: 5px; 
 
} 
 
div.basic_title { 
 
    position: relative; 
 
    width: 100%; 
 
    background-color: #005eff; 
 
    padding: 4px; 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
} 
 
div.basic_title p { 
 
    margin: 0px; 
 
    pointer-events: none; 
 
} 
 
div.basic_panel div.basic_title { 
 
    font-size: 14px; 
 
    font-weight: bold; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
 
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
 
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> 
 
<div id="bottom_bar"> 
 
    <div class="chat_box dark_container"> 
 
    <div class="basic_title"> 
 
     <p>Chat Box</p> 
 
     <div class="close_btn" onclick="minimize(0)"></div> 
 
    </div> 
 
    <div class="body"></div> 
 
    </div> 
 
    <div class="chat_bar chat_box_maximized"> 
 
    <p>Chat Box</p> 
 
    <form> 
 
     <input type="text" placeholder="send a message"> 
 
    </form> 
 
    </div> 
 
</div>

+0

豈不是更好地彙報這個bug給jQuery開發團隊? https://bugs.jqueryui.com/ – jeff

+0

嗯,我不是100%確定這是否是我做錯了,可以通過更改我的代碼來解決。但是,如果這肯定是他們的錯誤,我可以報告。 – Mayron

回答

1

當調整從頂部手柄,它改變頂部協調和高度。由於您將位置設置爲底部,因此通常在動畫上高度將會改變,但不是底部座標。但只要可調整大小集頂部座標,那麼動畫將作出,但與頂部座標剩餘。

你可以做的是使用調整回調,以防止頂部座標設定當你調整。然後它會保持動畫的正確方向,並且調整大小也會起作用。

$(document).ready(function() { 
 
    // controls resizing of the chat box 
 
    $('.chat_box').resizable({ 
 
    handles: 'n, e, ne', 
 
    minWidth: 300, 
 
    minHeight: 100, 
 
    maxWidth: 700, 
 
    maxHeight: 500, 
 
    resize: function(event, ui) { 
 
     ui.helper.css('top', ''); 
 
    } 
 
    }); 
 
}); 
 

 
function minimize(chatId) { 
 
    var bottom_bar = document.getElementById("bottom_bar"); 
 
    var box = bottom_bar.getElementsByClassName("chat_box")[chatId]; 
 
    var bar = bottom_bar.getElementsByClassName("chat_bar")[chatId]; 
 
    bar.className = "chat_bar chat_box_minimized"; 
 

 
    $(box).stop().animate({ 
 
     height: "0px", 
 
     width: bar.offsetWidth, 
 
    }, 
 
    'normal', function() { 
 
     $(box).hide(); 
 
    } 
 
); 
 
}
#bottom_bar { 
 
    position: fixed; 
 
    z-index: 10; 
 
    bottom: 0px; 
 
    left: 0px; 
 
    right: 0px; 
 
    max-height: 40px; 
 
    background-color: #0042b3; 
 
    padding: 2px 20px; 
 
} 
 
div.chat_box { 
 
    position: fixed; 
 
    width: 350px; 
 
    height: 180px; 
 
    margin: 0px 4px; 
 
    bottom: 45px; 
 
} 
 
div.close_btn { 
 
    position: absolute; 
 
    top: 0px; 
 
    right: 0px; 
 
    width: 30px; 
 
    height: 100%; 
 
} 
 
div.close_btn:before { 
 
    content: 'x'; 
 
    display: block; 
 
    text-align: center; 
 
    vertical-align: middle; 
 
    line-height: 25px; 
 
    font-weight: bold; 
 
    font-family: Arial, sans-serif; 
 
    pointer-events: none; 
 
} 
 
div.close_btn:hover { 
 
    background-color: rgba(0, 9, 26, 0.8); 
 
    cursor: pointer; 
 
} 
 
div.chat_box_maximized { 
 
    background-color: white; 
 
    width: 350px; 
 
    margin: 5px 0px; 
 
    padding: 2px; 
 
    border: 3px solid #0045cc; 
 
    border-radius: 5px; 
 
    display: inline-block; 
 
} 
 
div.chat_box_maximized input { 
 
    width: 100%; 
 
    border: none; 
 
} 
 
div.chat_box_maximized p { 
 
    display: none; 
 
} 
 
div.chat_box_minimized { 
 
    background-color: #002266; 
 
    ; 
 
    max-width: 200px; 
 
    min-width: 80px; 
 
    margin: 5px 0px; 
 
    padding: 2px; 
 
    border: 3px solid #002266; 
 
    ; 
 
    border-radius: 5px; 
 
    display: inline-block; 
 
} 
 
div.chat_box_minimized:hover { 
 
    background-color: #3378ff; 
 
    border: 3px solid #3378ff; 
 
    cursor: pointer; 
 
} 
 
div.chat_box_minimized form { 
 
    display: none; 
 
} 
 
div.chat_box_minimized p { 
 
    margin: 0px 5px; 
 
    font-size: 10pt; 
 
    color: white; 
 
    font-weight: bold; 
 
    pointer-events: none; 
 
} 
 
.light_container, 
 
.dark_container { 
 
    -webkit-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57); 
 
    -moz-box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57); 
 
    box-shadow: 0px 0px 4px 2px rgba(0, 0, 0, 0.57); 
 
    border: 1px solid #005eff; 
 
    padding: 1px; 
 
} 
 
.light_container { 
 
    background-color: rgba(0, 34, 102, 0.9); 
 
} 
 
.dark_container { 
 
    background-color: rgba(0, 9, 26, 0.9); 
 
} 
 
.light_container .body, 
 
.dark_container .body { 
 
    padding: 5px; 
 
} 
 
div.basic_title { 
 
    position: relative; 
 
    width: 100%; 
 
    background-color: #005eff; 
 
    padding: 4px; 
 
    box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    -webkit-box-sizing: border-box; 
 
} 
 
div.basic_title p { 
 
    margin: 0px; 
 
    pointer-events: none; 
 
} 
 
div.basic_panel div.basic_title { 
 
    font-size: 14px; 
 
    font-weight: bold; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
}
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
 
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script> 
 
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> 
 
<div id="bottom_bar"> 
 
    <div class="chat_box dark_container"> 
 
    <div class="basic_title"> 
 
     <p>Chat Box</p> 
 
     <div class="close_btn" onclick="minimize(0)"></div> 
 
    </div> 
 
    <div class="body"></div> 
 
    </div> 
 
    <div class="chat_bar chat_box_maximized"> 
 
    <p>Chat Box</p> 
 
    <form> 
 
     <input type="text" placeholder="send a message"> 
 
    </form> 
 
    </div> 
 
</div>

+0

非常感謝!我對「ui.helper」沒有經驗,不確定它做了什麼。知道的非常有幫助。 – Mayron