/    Sign up×
Community /Pin to ProfileBookmark

Grab and Move

I’ve got a parent DIV (say 1000px wide). Inside this I have a child DIV which has a child table (say 1500px wide)

Overflow-y scroll.

How can I make it so if I click and drag the table, I can move it without using the scroll bar?

Any CSS, vanilla JS or last resort jQuery type options?

to post a comment
CSSJavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumApr 09.2021 — Making the table draggable is easy, however when one considers it to be necessary to limit the movement it grows more complex. Some time ago I implemented this for an image and used jQuery-UI:
``<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="de"&gt;

&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;
&lt;meta charset="UTF-8"&gt;
&lt;title&gt;Testseite&lt;/title&gt;
&lt;script src="https://code.jquery.com/jquery-1.12.4.js"&gt;&lt;/script&gt;
&lt;script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="window" style="width: 250px; height: 250px; overflow: hidden; position: relative; margin: auto"&gt;
&lt;div id="wrapper" style="position: absolute"&gt;&lt;img id="draggable"
src="http://ulrichbangert.de/heimat/2013-08-05_Wolfenbuettel_07.jpg"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
$draggableele = $("#draggable");
$windowele = $("#window");
$draggableele.on('load', event =&gt; {
$draggableele.draggable();
wdiff = $draggableele.width() - $windowele.width();
hdiff = $draggableele.height() - $windowele.height();
$("#wrapper").css({
width: $windowele.width() + 2 * wdiff + "px",
height: $windowele.height() + 2 * hdiff + "px",
left: -wdiff + "px",
top: -hdiff + "px"
});
$draggableele.draggable({ containment: "parent" });
})
&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;<i>
</i>
``

jQuery-UI features a containment to limit the movement. This demo can be modified easily for moving a table.
Copy linkTweet thisAlerts:
@kiwisauthorApr 09.2021 — @Sempervivum#1630162

I get no errors and nothing works.

Inside the $draggableele.on('load', event => { event I console log a Hello World message and it doesn't get printed.

Have jQuery installed. No errors - any ideas?
Copy linkTweet thisAlerts:
@SempervivumApr 10.2021 — Did you replace the image by your table? Then you have to omit the load event:

` $draggableele.on('load', event =&gt; {</C><br/>
and the closing <C>
})`
.
Copy linkTweet thisAlerts:
@kiwisauthorApr 10.2021 — @Sempervivum#1630169

This is my code

#gamesTableTeamView = 1377pm wide

Parent: #gamesTableTeamView_wrapper is 1200px wide

Parent of that: #gamesTable is 1200px wide also

$draggableele = $("#gamesTableTeamView");<br/>
$windowele = $("#gamesTableTeamView_wrapper");<br/>
$draggableele.on('load', event =&gt; {<br/>
$draggableele.draggable();<br/>
wdiff = $draggableele.width() - $windowele.width();<br/>
hdiff = $draggableele.height() - $windowele.height();<br/>
$("#gamesTable").css({<br/>
width: $windowele.width() + 2 * wdiff + "px",<br/>
height: $windowele.height() + 2 * hdiff + "px",<br/>
left: -wdiff + "px",<br/>
top: -hdiff + "px"<br/>
});<br/>
$draggableele.draggable({ containment: "parent" });<br/>
})
Copy linkTweet thisAlerts:
@kiwisauthorApr 10.2021 — I am using DataTable, perhaps there's a conflict but I thought that would throw an error
Copy linkTweet thisAlerts:
@SempervivumApr 10.2021 — In your latest code there is still the listener for the load event.
Copy linkTweet thisAlerts:
@kiwisauthorApr 10.2021 — @Sempervivum#1630172

Okay, why remove the onload?

I now have this, I can move it up and down about 10px but not left to right

$draggableele = $("#gamesTableTeamView");
$windowele = $("#gamesTableTeamView_wrapper");
$draggableele.draggable();
wdiff = $draggableele.width() - $windowele.width();
hdiff = $draggableele.height() - $windowele.height();
$("#gamesTable").css({
width: $windowele.width() + 2 * wdiff + "px",
height: $windowele.height() + 2 * hdiff + "px",
left: -wdiff + "px",
top: -hdiff + "px"
});
$draggableele.draggable({ containment: "parent" });
Copy linkTweet thisAlerts:
@SempervivumApr 10.2021 — @kiwis80#1630193 A table doesn't have a load event as it's only HTML text, nothing to be downloaded.

In the meantime I created a test file and found out that it's necessary to create an additional wrapper for the table as Datatables adds some additional elements. This is my test file:
``<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="de"&gt;

&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;
&lt;meta charset="UTF-8"&gt;
&lt;title&gt;Draggable Table&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css"&gt;
&lt;script src="https://code.jquery.com/jquery-1.12.4.js"&gt;&lt;/script&gt;
&lt;script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"&gt;&lt;/script&gt;
&lt;style&gt;
#window {
width: 500px;
height: 500px;
overflow: hidden;
position: relative;
margin: auto
}

#wrapper {
position: absolute;
}

#draggable {
width: 800px;
height: auto;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="window"&gt;
&lt;div id="wrapper"&gt;
&lt;div id="draggable"&gt;
&lt;table id="the-table" class="display" cellspacing="0" width="800px"&gt;
&lt;!-- table data here --&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
$draggableele = $("#draggable");
$windowele = $("#window");
$('#the-table').DataTable({ paging: false });
// $draggableele.on('load', event =&gt; {
$draggableele.draggable();
wdiff = $draggableele.width() - $windowele.width();
hdiff = $draggableele.height() - $windowele.height();
$("#wrapper").css({
width: $windowele.width() + 2 * wdiff + "px",
height: $windowele.height() + 2 * hdiff + "px",
left: -wdiff + "px",
top: -hdiff + "px"
});
$draggableele.draggable({ containment: "parent" });
// })
&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@kiwisauthorApr 10.2021 — Here's what I've got. Not working

https://jsfiddle.net/4fdu93ht/2/

You'll see I have multiple .mytables DIV's. There's all have different tables, I'm using JS to add/remove the .fullHeight class to show or hide the content.
Copy linkTweet thisAlerts:
@SempervivumApr 11.2021 — @kiwis80#1630200
>You'll see I have multiple .mytables DIV's.

I'm unsure what your intention is: Do you want to make each table draggable on it's own or all tables together?
Copy linkTweet thisAlerts:
@kiwisauthorApr 11.2021 — @Sempervivum#1630205

Only one table is wider than the container. If a second gets wider I'll add extra code.
Copy linkTweet thisAlerts:
@SempervivumApr 11.2021 — The script had some trouble when one of the diff's was negative. Had to fix this.

You addressed the table by this: `$('#the-table').DataTable</C>, however the table tag didn't have such id.<br/>
I modified the script a bit and added some CSS. This is working for me now:
<CODE>
`<i>
</i>&lt;!DOCTYPE html&gt;
&lt;html lang="de"&gt;

&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"&gt;
&lt;meta charset="UTF-8"&gt;
&lt;title&gt;Draggable Table&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css"&gt;
&lt;script src="https://code.jquery.com/jquery-1.12.4.js"&gt;&lt;/script&gt;
&lt;script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"&gt;&lt;/script&gt;
&lt;script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"&gt;&lt;/script&gt;
&lt;style&gt;
.container {
max-width: 500px;
margin: 0 auto;
display: flex;
flex-direction: column;
}

.mytables {
height: 0;
overflow: hidden;
position: relative;
}

.fullheight {
height: 100% !important;
}

table {
width: 100%;
max-width: 1200px;
margin: 1em auto;
padding: 0;
border-collapse: collapse;
box-sizing: border-box;
white-space: nowrap;
border: solid 1px #000;
cursor: grab;
}

td,
th {
background: #f3f0f0;
font-weight: 200;
padding: 0.4em;
font-size: 0.8em;
border: solid 1px rgb(136 136 136);
white-space: nowrap;
text-align: center;
box-sizing: border-box;
}
&lt;/style&gt;
&lt;style&gt;
#window {
width: 500px;
height: 500px;
overflow: hidden;
position: relative;
margin: auto
}

#wrapper {
position: absolute;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div class="container" id="window"&gt;
&lt;div class="tables" id="wrapper"&gt;
&lt;div class="mytables"&gt;

&lt;/div&gt;
&lt;div class="mytables"&gt;

&lt;/div&gt;
&lt;div class="mytables fullheight" id="draggable"&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;22 22 22 22 22 22 22 22&lt;/td&gt;
&lt;td&gt;22 22 22 22 22 22 22 22&lt;/td&gt;
&lt;td&gt;22 22 22 22 22 22 22 22&lt;/td&gt;
&lt;td&gt;22 22 22 22 22 22 22 22&lt;/td&gt;
&lt;td&gt;22 22 22 22 22 22 22 22&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
$draggableele = $("#draggable");
$windowele = $("#window");
$tableele = $('table');
$tableele.DataTable({
paging: false,
initComplete: function (settings, json) {
$draggableele.width($tableele.width());
if ($windowele.height() &gt; $draggableele.height())
$windowele.height($draggableele.height());
$draggableele.draggable({ containment: "parent" });
wdiff = $draggableele.width() - $windowele.width();
if (wdiff &lt; 0) wdiff = 0;
hdiff = $draggableele.height() - $windowele.height();
if (hdiff &lt; 0) hdiff = 0;
$("#wrapper").css({
width: $windowele.width() + 2 * wdiff + "px",
height: $windowele.height() + 2 * hdiff + "px",
left: -wdiff + "px",
top: -hdiff + "px"
});
}
});
&lt;/script&gt;
&lt;/body&gt;

&lt;/html&gt;<i>
</i>
``
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...