gpt4 book ai didi

javascript - 如何通过右栏调整大小

转载 作者:行者123 更新时间:2023-11-28 03:12:48 24 4
gpt4 key购买 nike

我正在创建一个带有左右面板的可调整大小的仪表板,通过左侧面板,它工作正常,但通过右侧面板无法做到这一点,如何做到这一点?

我正在创建一个带有左右面板的可调整大小的仪表板,通过左侧面板,它工作正常,但通过右侧面板我无法做到这一点,如何做到这一点?

这是我的代码

   var min = 300;
var max = 3600;
var mainmin = 200;

$('#projectsplit-bar').mousedown(function (e) {
e.preventDefault();
$(document).mousemove(function (e) {
e.preventDefault();
var x = e.pageX - $('#projectsidebar').offset().left;

if (x > min && x < max && e.pageX < ($(window).width() - mainmin)) {
$('#projectsidebar').css("width", x);
$('#projectmain').css("margin-left", x);
}



})
});

$('#rightsplitbar').mousedown(function (e) {

e.preventDefault();
$(document).mousemove(function (e) {
e.preventDefault();

var y = e.pageX - $('#rightbar').offset().right;
if (y > min && y < max && e.pageX < ($(window).width() - mainmin)) {
$('#projectsidebar').css("width", y);
$('#projectmain').css("margin-right", y);
}



})
});
$(document).mouseup(function (e) {
$(document).unbind('mousemove');
});
  <html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<style>
@import "compass/css3";


#projectmain {
background-color: BurlyWood;
margin-left: 300px;
margin-right: 300px;
height: 300px;
}
#rightbar {
background-color: yellow;
width: 350px;
float: right;
height: 300px;
}


#projectsidebar {
background-color: IndianRed;
width: 350px;
float: left;
height: 300px;
}

#projectsplit-bar {
background-color:black;
height:100%;
float: right;
width: 6px;
cursor: col-resize;
}

#rightsplitbar {
background-color:black;
height:100%;
float: left;
width: 6px;
cursor: col-resize;
}
</style>



</head>
<body>


<div id="projectsidebar">
<div id="projectsplit-bar"></div>
left
</div>
<div id="rightbar">
<div id="rightsplitbar"></div>
rightbar
</div>
<div id="projectmain">main</div>
</body>
</html>

最佳答案

尝试使用下面的代码。问题是 jQuery 的 offset 函数没有正确的属性。仅存在顶部和左侧属性。我简单地恢复了 X 轴。

var min = 300;
var max = 3600;
var mainmin = 200;

$('#projectsplit-bar').mousedown(function (e) {
e.preventDefault();
$(document).mousemove(function (e) {
e.preventDefault();
var x = e.pageX - $('#projectsidebar').offset().left;
if (x > min && x < max && e.pageX < ($(window).width() - mainmin)) {
$('#projectsidebar').css("width", x);
$('#projectmain').css("margin-left", x);
}
})
});

$('#rightsplitbar').mousedown(function (e) {

e.preventDefault();
$(document).mousemove(function (e) {
e.preventDefault();
var y = ($(window).width() - e.pageX) - 8;
if (y > min && y < max && e.pageX < ($(window).width() - mainmin)) {
$('#rightbar').css("width", y);
$('#projectmain').css("margin-right", y);
}
})
});
$(document).mouseup(function (e) {
$(document).unbind('mousemove');
});
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<style>
@import "compass/css3";


#projectmain {
background-color: BurlyWood;
margin-left: 300px;
margin-right: 300px;
height: 300px;
}
#rightbar {
background-color: yellow;
width: 350px;
float: right;
height: 300px;
}


#projectsidebar {
background-color: IndianRed;
width: 350px;
float: left;
height: 300px;
}

#projectsplit-bar {
background-color:black;
height:100%;
float: right;
width: 6px;
cursor: col-resize;
}

#rightsplitbar {
background-color:black;
height:100%;
float: left;
width: 6px;
cursor: col-resize;
}
</style>
</head>
<body>

<div id="projectsidebar">
<div id="projectsplit-bar"></div>
left
</div>
<div id="rightbar">
<div id="rightsplitbar"></div>
rightbar
</div>
<div id="projectmain">main</div>
</body>
</html>

关于javascript - 如何通过右栏调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59948315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com