helper.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var helper = {};
  2. helper.createDiv = function() {
  3. return document.createElement('div');
  4. };
  5. helper.toHtmlObject = function(htmlCode) {
  6. var div = helper.createDiv();
  7. div.innerHTML = htmlCode;
  8. return div.firstChild;
  9. };
  10. helper.removeClass = function(obj, className) {
  11. if(helper.hasClass(obj, className)) {
  12. var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
  13. obj.className = obj.className.replace(reg, ' ').trim();
  14. };
  15. };
  16. helper.hasScrollBar = function(tag) {
  17. return (Math.max(tag.scrollHeight, tag.offsetHeight) > window.innerHeight);
  18. };
  19. helper.hasClass = function(obj, className) {
  20. return !!obj.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)'));
  21. };
  22. helper.getScrollWidth = function() {
  23. var outer = helper.createDiv();
  24. outer.style.visibility = 'hidden';
  25. outer.style.width = '100px';
  26. outer.style.msOverflowStyle = 'scrollbar';
  27. document.body.appendChild(outer);
  28. var widthNoScroll = outer.offsetWidth;
  29. outer.style.overflow = 'scroll';
  30. var inner = helper.createDiv();
  31. inner.style.width = '100%';
  32. outer.appendChild(inner);
  33. var widthWithScroll = inner.offsetWidth;
  34. outer.parentNode.removeChild(outer);
  35. return widthNoScroll - widthWithScroll;
  36. };
  37. helper.addClass = function(obj, className) {
  38. if(!helper.hasClass(obj, className)) obj.className += " " + className;
  39. };