You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

7 years ago
  1. (function($) {
  2. "use strict";
  3. // jQuery plugin definition
  4. $.fn.TextAreaExpander = function(minHeight, maxHeight) {
  5. var resize=function(target,minHeight,maxHeight){
  6. // 保存初始高度,之后需要重新设置一下初始高度,避免只能增高不能减低。
  7. var dh = $(target).attr('defaultHeight') || minHeight;
  8. if(minHeight && dh<minHeight){
  9. dh=minHeight;
  10. }
  11. if (!dh) {
  12. dh = target.clientHeight || minHeight;
  13. if(minHeight && dh>minHeight){
  14. dh=minHeight;
  15. }
  16. $(target).attr('defaultHeight', dh);
  17. }
  18. target.style.height = dh +'px';
  19. var clientHeight = target.clientHeight || minHeight;
  20. var scrollHeight = target.scrollHeight || maxHeight;
  21. if(scrollHeight>maxHeight){
  22. scrollHeight=maxHeight;
  23. }else if($(target).is(':hidden')){
  24. scrollHeight=minHeight;
  25. }
  26. if (clientHeight !== scrollHeight) {
  27. target.style.height = scrollHeight + 2 + "px";
  28. }
  29. };
  30. this.off(".TextAreaExpander").on("input.TextAreaExpander propertychange.TextAreaExpander", function () {
  31. resize(this,minHeight,maxHeight);
  32. }).on('focus',function(){
  33. resize(this,minHeight,maxHeight);
  34. });
  35. this.each(function(){
  36. resize(this,minHeight,maxHeight);
  37. });
  38. return this;
  39. };
  40. })(jQuery);