如何将 jquery-jscrollpane 中的滚动条移动到文本区域光标位置

发布于 2024-12-21 18:12:42 字数 3232 浏览 1 评论 0 原文

我尝试在 jsfiddle 中实现。当文本区域展开滚动条时,不会移动到文本区域光标。

function addJScrollPaneTo($container) {
  // use jScrollPane jquery plugin
  $container.jScrollPane({
    verticalGutter: 1,
    stickToBottom: true,
    enableKeyboardNavigation: false,
    autoReinitialise: true
  });
}

function appendTextareaTo($container) {

  var $textarea = $("<textarea />");

  // set textarea attribute
  $textarea.css({
    'border': 'medium none'
  });
  $textarea.width($container.width());

  // use TextAreaExpander jquery plugin
  $textarea.TextAreaExpander($container.height() - 1);

  $container.append($textarea);
}

// for padding with elem
function appendContainer(elem) {

  var $container = $("<div />");

  // set container attribute
  $container.width($(elem).width());
  $container.height($(elem).height());

  $(elem).append($container);
  appendTextareaTo($container);
  addJScrollPaneTo($container);
}

function setCSS(elem) {
  $(elem).css({
    'border': 'thin solid #CCCCCC',
    'padding': '1px'
  });
}

setCSS("#test");
appendContainer("#test");
.jspContainer {
  overflow: hidden;
  position: relative;
}

.jspPane {
  position: absolute;
}

.jspVerticalBar {
  position: absolute;
  top: 0;
  right: 0;
  width: 16px;
  height: 100%;
  background: red;
}

.jspHorizontalBar {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 16px;
  background: red;
}

.jspVerticalBar *,
.jspHorizontalBar * {
  margin: 0;
  padding: 0;
}

.jspCap {
  display: none;
}

.jspHorizontalBar .jspCap {
  float: left;
}

.jspTrack {
  background: #dde;
  position: relative;
}

.jspDrag {
  background: #bbd;
  position: relative;
  top: 0;
  left: 0;
  cursor: pointer;
}

.jspHorizontalBar .jspTrack,
.jspHorizontalBar .jspDrag {
  float: left;
  height: 100%;
}

.jspArrow {
  background: #50506d;
  text-indent: -20000px;
  display: block;
  cursor: pointer;
}

.jspArrow.jspDisabled {
  cursor: default;
  background: #80808d;
}

.jspVerticalBar .jspArrow {
  height: 16px;
}

.jspHorizontalBar .jspArrow {
  width: 16px;
  float: left;
  height: 100%;
}

.jspVerticalBar .jspArrow:focus {
  outline: none;
}

.jspCorner {
  background: #eeeef4;
  float: left;
  height: 100%;
}


/* Yuk! CSS Hack for IE6 3 pixel bug :( */

* html .jspCorner {
  margin: 0 -3px 0 0;
}

.jspVerticalBar {
  background: none;
  border-radius: 5px 5px 5px 5px;
  right: 0px;
  width: 5px;
}

.jspScrollable .jspHorizontalBar {
  background: none;
  bottom: 0px;
  height: 10px;
  border-radius: 5px 5px 5px 5px;
}

.jspScrollable .jspTrack {
  background: none repeat scroll 0 0 #D1D3D5;
  border-radius: 5px 5px 5px 5px;
}

.jspScrollable .jspDrag {
  background: none repeat scroll 0 0 #5E6063;
  border-radius: 5px 5px 5px 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="test" style="width: 235px; height: 50px;"></div>

I try to implement in jsfiddle. When textarea expand scrollbar not move to textarea cursor.

function addJScrollPaneTo($container) {
  // use jScrollPane jquery plugin
  $container.jScrollPane({
    verticalGutter: 1,
    stickToBottom: true,
    enableKeyboardNavigation: false,
    autoReinitialise: true
  });
}

function appendTextareaTo($container) {

  var $textarea = $("<textarea />");

  // set textarea attribute
  $textarea.css({
    'border': 'medium none'
  });
  $textarea.width($container.width());

  // use TextAreaExpander jquery plugin
  $textarea.TextAreaExpander($container.height() - 1);

  $container.append($textarea);
}

// for padding with elem
function appendContainer(elem) {

  var $container = $("<div />");

  // set container attribute
  $container.width($(elem).width());
  $container.height($(elem).height());

  $(elem).append($container);
  appendTextareaTo($container);
  addJScrollPaneTo($container);
}

function setCSS(elem) {
  $(elem).css({
    'border': 'thin solid #CCCCCC',
    'padding': '1px'
  });
}

setCSS("#test");
appendContainer("#test");
.jspContainer {
  overflow: hidden;
  position: relative;
}

.jspPane {
  position: absolute;
}

.jspVerticalBar {
  position: absolute;
  top: 0;
  right: 0;
  width: 16px;
  height: 100%;
  background: red;
}

.jspHorizontalBar {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 16px;
  background: red;
}

.jspVerticalBar *,
.jspHorizontalBar * {
  margin: 0;
  padding: 0;
}

.jspCap {
  display: none;
}

.jspHorizontalBar .jspCap {
  float: left;
}

.jspTrack {
  background: #dde;
  position: relative;
}

.jspDrag {
  background: #bbd;
  position: relative;
  top: 0;
  left: 0;
  cursor: pointer;
}

.jspHorizontalBar .jspTrack,
.jspHorizontalBar .jspDrag {
  float: left;
  height: 100%;
}

.jspArrow {
  background: #50506d;
  text-indent: -20000px;
  display: block;
  cursor: pointer;
}

.jspArrow.jspDisabled {
  cursor: default;
  background: #80808d;
}

.jspVerticalBar .jspArrow {
  height: 16px;
}

.jspHorizontalBar .jspArrow {
  width: 16px;
  float: left;
  height: 100%;
}

.jspVerticalBar .jspArrow:focus {
  outline: none;
}

.jspCorner {
  background: #eeeef4;
  float: left;
  height: 100%;
}


/* Yuk! CSS Hack for IE6 3 pixel bug :( */

* html .jspCorner {
  margin: 0 -3px 0 0;
}

.jspVerticalBar {
  background: none;
  border-radius: 5px 5px 5px 5px;
  right: 0px;
  width: 5px;
}

.jspScrollable .jspHorizontalBar {
  background: none;
  bottom: 0px;
  height: 10px;
  border-radius: 5px 5px 5px 5px;
}

.jspScrollable .jspTrack {
  background: none repeat scroll 0 0 #D1D3D5;
  border-radius: 5px 5px 5px 5px;
}

.jspScrollable .jspDrag {
  background: none repeat scroll 0 0 #5E6063;
  border-radius: 5px 5px 5px 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="test" style="width: 235px; height: 50px;"></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文