javascript - On div scroll activate another div's scroll -
i trying activate current scroll while outside scroll, in #divdet
here tried:
$("div#detdiv").scroll(function () { //i don't know should have here // $("div#scrldiv").scroll(); });
thank you
it sounds want respond scroll on 1 div
scrolling another.
you've determined how hook scroll
event. set scroll position of element (the other div
), set element's scrolltop
, scrollleft
values (which in pixels). if want 2 divs scroll in near-unison, instance, you'd assign source div's scrolltop
, scrollleft
target div
.
relevant javascript:
(function() { var target = $("#target"); $("#source").scroll(function() { target.prop("scrolltop", this.scrolltop) .prop("scrollleft", this.scrollleft); }); })();
or alternately (source):
(function() { var target = $("#target")[0]; // <== getting raw element $("#source").scroll(function() { target.scrolltop = this.scrolltop; target.scrollleft = this.scrollleft; }); })();
full page:
<!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <meta charset=utf-8 /> <title>scroll example</title> <style> .scroll-example { display: inline-block; width: 40%; border: 1px solid black; margin-right: 20px; height: 100px; overflow: scroll; } </style> </head> <body> <p>scroll left div, watch right one.</p> <div id="source" class="scroll-example"> 1 <br>2 <br>3 <br>4 <br>5 <br>6 <br>7 <br>8 <br>9 <br>10 <br>11 <br>12 <br>13 <br>14 <br>15 <br>16 <br>17 <br>18 <br>19 <br>20 </div> <div id="target" class="scroll-example"> 1 <br>2 <br>3 <br>4 <br>5 <br>6 <br>7 <br>8 <br>9 <br>10 <br>11 <br>12 <br>13 <br>14 <br>15 <br>16 <br>17 <br>18 <br>19 <br>20 </div> <script> (function() { var target = $("#target"); $("#source").scroll(function() { target.prop("scrolltop", this.scrolltop) .prop("scrollleft", this.scrollleft); }); })(); </script> </body> </html>
Comments
Post a Comment