|
895 | 895 | }; |
896 | 896 |
|
897 | 897 | /** |
898 | | - * Sets the given style (css) property of the element with the given value. |
899 | | - * If no value is specified, returns the value of the given property, |
900 | | - * or undefined if the property is not. |
| 898 | + * Sets the given style (css) property (1st arg) of the element with the |
| 899 | + * given value (2nd arg). If a single argument is given, .style() |
| 900 | + * returns the value of the given property; however, if the single argument |
| 901 | + * is given in css syntax ('text-align:center'), .style() sets the css |
| 902 | + * appropriatly. .style() also handles 2d and 3d css transforms. If |
| 903 | + * the 1st arg is 'rotate', 'translate', or 'position', the following arguments |
| 904 | + * accept Numbers as values. ('translate', 10, 100, 50); |
901 | 905 | * |
902 | 906 | * @method style |
903 | 907 | * @param {String} property property to be set |
904 | | - * @param {String} [value] value to assign to property |
| 908 | + * @param {String|Number} [value] value to assign to property |
| 909 | + * @param {String|Number} [value] value to assign to property (rotate/translate) |
| 910 | + * @param {String|Number} [value] value to assign to property (rotate/translate) |
| 911 | + * @param {String|Number} [value] value to assign to property (translate) |
905 | 912 | * @return {String|Object/p5.Element} value of property, if no value is specified |
906 | | - * or p5.Element |
| 913 | + * or p5.Element |
907 | 914 | * @example |
908 | 915 | * <div><code class="norender"> |
909 | 916 | * var myDiv = createDiv("I like pandas."); |
|
915 | 922 | var self = this; |
916 | 923 |
|
917 | 924 | if (typeof val === 'undefined') { |
918 | | - if (!prop.match(/:/)) { |
| 925 | + if (prop.indexOf(':') === -1) { |
919 | 926 | var styles = window.getComputedStyle(self.elt); |
920 | 927 | var style = styles.getPropertyValue(prop); |
921 | 928 | return style; |
|
929 | 936 | } |
930 | 937 | } |
931 | 938 | } else { |
932 | | - if (prop.match(/rotate/)) { |
| 939 | + if (prop === 'rotate'){ |
933 | 940 | if (arguments.length === 2) { |
934 | 941 | var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
935 | 942 | style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
|
948 | 955 | this.elt.style.transform += 'rotateZ(' + arguments[2] + 'deg)'; |
949 | 956 | this.elt.style.transform += style; |
950 | 957 | } |
951 | | - } else if (prop.match(/translate/)) { |
| 958 | + } else if (prop === 'translate') { |
952 | 959 | if (arguments.length === 3) { |
953 | 960 | var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
954 | 961 | style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
|
967 | 974 | this.elt.style.transform += style; |
968 | 975 | this.elt.parentElement.style.perspective = arguments[3] + 'px'; |
969 | 976 | } |
970 | | - } else if (prop.match(/position/)) { |
| 977 | + } else if (prop === 'position') { |
971 | 978 | this.elt.style.left = arguments[1] + 'px'; |
972 | 979 | this.elt.style.top = arguments[2] + 'px'; |
973 | 980 | this.x = arguments[1]; |
|
976 | 983 | this.elt.style[prop] = val; |
977 | 984 | if (prop === 'width' || prop === 'height' || prop === 'left' || prop === 'top') { |
978 | 985 | var numVal = val.replace(/\D+/g, ''); |
979 | | - this[prop] = parseInt(numVal); |
| 986 | + this[prop] = parseInt(numVal, 10); |
980 | 987 | } |
981 | 988 | } |
982 | 989 | } |
|
0 commit comments