сталкивается с проблемой для исправления положения компонентов слева и справа - PullRequest
0 голосов
/ 05 апреля 2020

Я работаю над небольшим компонентом, с которым я сталкиваюсь, чтобы контролировать перемещение div. я хочу остановить перемещение div, когда он достигает левой или правой стороны, или другими словами, он должен остановиться, когда слева или справа == 0;

я работаю над небольшим компонентом, с которым я сталкиваюсь, чтобы управлять перемещением div , я хочу прекратить перемещение div, когда он достигает левой или правой стороны, или другими словами, он должен остановиться, когда слева или справа == 0;

var leftDivWidth  = $('#left').width()/2;
applyRect = $('#left').css('clip', 'rect(0px,' + leftDivWidth + 'px, auto,0px)');
var isResizing = false,
lastDownX = 0;
$(function () {
    var container = $('#container'),
        left = $('#left'),
        handle = $('#handle');
    handle.on('mousedown', function (e) {
        isResizing = true;
        lastDownX = e.clientX;
    });
    $(document).on('mousemove', function (e) {
        if (!isResizing) 
            return;
	        var offsetRight = e.clientX - container.offset().left;
            if($(handle).left == 0){
                alert('working');
            }
	        handle.css('left', offsetRight);
	        left.css('clip', 'rect(0px,' + offsetRight + 'px,auto,0px)')
    }).on('mouseup', function (e) {
        isResizing = false;
    });
});
body, html {
    width: 100%;
    margin: 0;
    padding: 0;
    position: relative;
}
#container {
    position: relative;
    width: 100%;
    margin: 0 auto;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: moz-none;
    -ms-user-select: none;
    user-select: none;
    overflow: hidden;
    max-width: 1200px;
}
#container #left {
    position: absolute;
    width: 100%;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 9;
    /*clip: rect(0px, 50%, 484px, 0px);*/
}
#container #handle {
    display: block;
    width: 50px;
    height: 50px;
    background: green;
    color: yellow;
    z-index: 999;
    font-size: 25px;
    line-height: 50px;
    text-align: center;
    border-radius: 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    cursor: pointer;
}
.fa{
	font-family: fontawesome;
    line-height: 52px;
}
#left img{
    width: 100%;
    position: absolute;
    top: 0;
    display: block;
}
#right img{
    width: 100%;
}
    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">


<div id="container">
        <div id="right">
            <img src="https://images.wallpaperscraft.com/image/astronaut_neon_art_148365_1920x1080.jpg">
        </div>
        <!-- Left side -->
        <div id="left"> 
        	<img src="https://images.wallpaperscraft.com/image/astronaut_ring_neon_156673_1920x1080.jpg">
        </div>
    	<div class="innerDiv">
    	    <div id="handle"> <i class="fa fa-arrows"></i> </div>
    	</div>
        <!-- Right side -->
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

Ответы [ 2 ]

0 голосов
/ 05 апреля 2020

Просто добавьте offset () к оператору if:

            if($(handle).left <= 0){
                alert('working');
            }

            if($(handle).offset().left <= 0){
                alert('working');
            }
И лучше использовать <=, чем строгое == уравнение. </p>
0 голосов
/ 05 апреля 2020

Просто сопоставьте его с parentOffsetWidth и, чтобы заблокировать движение, просто создайте флаг lockMovement и просто установите его в true, когда условие встречается.

var leftDivWidth  = $('#left').width()/2;
applyRect = $('#left').css('clip', 'rect(0px,' + leftDivWidth + 'px, auto,0px)');
var isResizing = false;
var lockMovement = false;
lastDownX = 0;
$(function () {
    var container = $('#container'),
        left = $('#left'),
        handle = $('#handle');
    handle.on('mousedown', function (e) {
        if(!lockMovement){
           isResizing = true;
           lastDownX = e.clientX;
        }
    });
    $(document).on('mousemove', function (e) {
        if (!isResizing) 
            return;
	        var offsetRight = e.clientX - container.offset().left;
            if($(handle)[0].offsetLeft >= $(handle)[0].offsetParent.offsetWidth){ //========> match the parent offsetwidth with the child offsetLeft
                alert('working');
                isResizing = false;
                lastDownX=0;
                lockMovement =true;
            }
	        handle.css('left', offsetRight);
	        left.css('clip', 'rect(0px,' + offsetRight + 'px,auto,0px)')
    }).on('mouseup', function (e) {
          isResizing = false;
    });
});
body, html {
    width: 100%;
    margin: 0;
    padding: 0;
    position: relative;
}
#container {
    position: relative;
    width: 100%;
    margin: 0 auto;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: moz-none;
    -ms-user-select: none;
    user-select: none;
    overflow: hidden;
    max-width: 1200px;
}
#container #left {
    position: absolute;
    width: 100%;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 9;
    /*clip: rect(0px, 50%, 484px, 0px);*/
}
#container #handle {
    display: block;
    width: 50px;
    height: 50px;
    background: green;
    color: yellow;
    z-index: 999;
    font-size: 25px;
    line-height: 50px;
    text-align: center;
    border-radius: 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    cursor: pointer;
}
.fa{
	font-family: fontawesome;
    line-height: 52px;
}
#left img{
    width: 100%;
    position: absolute;
    top: 0;
    display: block;
}
#right img{
    width: 100%;
}
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">


<div id="container">
        <div id="right">
            <img src="https://images.wallpaperscraft.com/image/astronaut_neon_art_148365_1920x1080.jpg">
        </div>
        <!-- Left side -->
        <div id="left"> 
        	<img src="https://images.wallpaperscraft.com/image/astronaut_ring_neon_156673_1920x1080.jpg">
        </div>
    	<div class="innerDiv">
    	    <div id="handle"> <i class="fa fa-arrows"></i> </div>
    	</div>
        <!-- Right side -->
    </div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
...