Guest User

Untitled

a guest
Jul 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. /*
  2. * margin-top or marginTop?
  3. * When to use lower-case and camel-case property names in JavaScript.
  4. */
  5.  
  6. // Setting style: camel-case
  7. elem.style.marginTop = '10px';
  8.  
  9. // Getting style: camel-case
  10. var value = elem.style.marginTop;
  11.  
  12. // Getting computed style: lower-case
  13. var value =
  14. window.getComputedStyle(elem).getPropertyValue( 'margin-top' );
  15.  
  16. /* Vendor prefixed properties are slightly different */
  17.  
  18. // Setting or getting style: camel-case with leading upper-case
  19. elem.style.MozTransform = "rotate(90deg)";
  20. // or WebkitTransform or OTransform
  21.  
  22. // Getting computed style: lower-case with leading dash
  23. var value =
  24. window.getComputedStyle(elem).getPropertyValue( '-moz-transform' );
  25. // or "-webkit-transform" or "-o-transform"
  26.  
  27. /* Trickier: setting the transition property */
  28.  
  29. // mixed camel-case and lower-case
  30. elem.style.MozTransition =
  31. "margin-top 1s ease, -moz-transform 1s linear";
  32.  
  33. /* Too complex?
  34. * No worries, JavaScript libraries can handle that for you
  35. */
  36.  
  37. // Setting style with jQuery: any form can be used
  38. $( elem ).css( 'margin-top', '10px' );
  39. // Getting computed style with jQuery: any form can be used
  40. $( elem ).css( 'marginTop' );
  41. // Even with vendor prefixed properties
  42. $( elem ).css( 'MozTransform' );
  43.  
  44. /* Conversion is actually really easy using regular expressions */
  45.  
  46. function camelCase( name ) {
  47. return name.replace(/-([a-z])/g, function( all, letter ) {
  48. return letter.toUpperCase();
  49. });
  50. }
  51.  
  52. function lowerCase( name ) {
  53. return name.replace(/([A-Z])/g, "-$1").toLowerCase();
  54. }
  55.  
  56. /*
  57. * They work both for "standard" properties AND vendor prefixed ones!
  58. * ...
  59. * Introducing IE9
  60. */
  61.  
  62. // Setting and Getting style: camel case without leading upper-case!
  63. elem.style.msTransform = "rotate(90deg)";
  64.  
  65. /* Thought dirty browser-specific workarounds were a thing of the past?
  66. * Think again.
  67. */
  68.  
  69. function fixedCamelCase( name ) {
  70. if ( name.indexOf('-ms-') === 0 ) {
  71. name = name.substr(1);
  72. }
  73. return name.replace(/-([a-z])/g, function( all, letter ) {
  74. return letter.toUpperCase();
  75. });
  76. }
  77.  
  78. function fixedLowerCase( name ) {
  79. return name.replace(/([A-Z]|^ms)/g, "-$1").toLowerCase();
  80. }
  81.  
  82. /* Thank god there are no style properties starting with "ms..." */
  83.  
  84. @louis_remi
Add Comment
Please, Sign In to add comment