|
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."); |
|
912 | 919 | * </code></div> |
913 | 920 | */ |
914 | 921 | p5.Element.prototype.style = function(prop, val) { |
| 922 | + var self = this; |
| 923 | + |
915 | 924 | if (typeof val === 'undefined') { |
916 | | - var attrs = prop.split(';'); |
917 | | - for (var i=0; i<attrs.length; i++) { |
918 | | - var parts = attrs[i].split(':'); |
919 | | - if (parts[0] && parts[1]) { |
920 | | - this.elt.style[parts[0].trim()] = parts[1].trim(); |
| 925 | + if (prop.indexOf(':') === -1) { |
| 926 | + var styles = window.getComputedStyle(self.elt); |
| 927 | + var style = styles.getPropertyValue(prop); |
| 928 | + return style; |
| 929 | + } else { |
| 930 | + var attrs = prop.split(';'); |
| 931 | + for (var i = 0; i < attrs.length; i++) { |
| 932 | + var parts = attrs[i].split(':'); |
| 933 | + if (parts[0] && parts[1]) { |
| 934 | + this.elt.style[parts[0].trim()] = parts[1].trim(); |
| 935 | + } |
921 | 936 | } |
922 | 937 | } |
923 | 938 | } else { |
924 | | - this.elt.style[prop] = val; |
925 | | - if (prop === 'width' || prop === 'height' || prop === 'left' || prop === 'top'){ |
926 | | - var numVal = val.replace(/\D+/g,''); |
927 | | - this[prop] = parseInt(numVal); |
| 939 | + if (prop === 'rotate'){ |
| 940 | + if (arguments.length === 2) { |
| 941 | + var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
| 942 | + style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
| 943 | + this.elt.style.transform = 'rotate(' + arguments[0] + 'deg)'; |
| 944 | + this.elt.style.transform += style; |
| 945 | + } else if (arguments.length === 3) { |
| 946 | + var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
| 947 | + style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
| 948 | + this.elt.style.transform = 'rotate(' + arguments[0] + 'deg, ' + arguments[1] + 'deg)'; |
| 949 | + this.elt.style.transform += style; |
| 950 | + } else if (arguments.length === 4) { |
| 951 | + var style = this.elt.style.transform.replace(/rotate3d\(.*\)/g, ''); |
| 952 | + style = style.replace(/rotate[X-Z]?\(.*\)/g, ''); |
| 953 | + this.elt.style.transform = 'rotateX(' + arguments[0] + 'deg)'; |
| 954 | + this.elt.style.transform += 'rotateY(' + arguments[1] + 'deg)'; |
| 955 | + this.elt.style.transform += 'rotateZ(' + arguments[2] + 'deg)'; |
| 956 | + this.elt.style.transform += style; |
| 957 | + } |
| 958 | + } else if (prop === 'translate') { |
| 959 | + if (arguments.length === 3) { |
| 960 | + var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
| 961 | + style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
| 962 | + this.elt.style.transform = 'translate(' + arguments[0] + 'px, ' + arguments[1] + 'px)'; |
| 963 | + this.elt.style.transform += style; |
| 964 | + } else if (arguments.length === 4) { |
| 965 | + var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
| 966 | + style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
| 967 | + this.elt.style.transform = 'translate3d(' + arguments[0] + 'px,' + arguments[1] + 'px,' + arguments[2] + 'px)'; |
| 968 | + this.elt.style.transform += style; |
| 969 | + this.elt.parentElement.style.perspective = '1000px'; |
| 970 | + } else if (arguments.length === 5) { |
| 971 | + var style = this.elt.style.transform.replace(/translate3d\(.*\)/g, ''); |
| 972 | + style = style.replace(/translate[X-Z]?\(.*\)/g, ''); |
| 973 | + this.elt.style.transform = 'translate3d(' + arguments[0] + 'px,' + arguments[1] + 'px,' + arguments[2] + 'px)'; |
| 974 | + this.elt.style.transform += style; |
| 975 | + this.elt.parentElement.style.perspective = arguments[3] + 'px'; |
| 976 | + } |
| 977 | + } else if (prop === 'position') { |
| 978 | + this.elt.style.left = arguments[1] + 'px'; |
| 979 | + this.elt.style.top = arguments[2] + 'px'; |
| 980 | + this.x = arguments[1]; |
| 981 | + this.y = arguments[2]; |
| 982 | + } else { |
| 983 | + this.elt.style[prop] = val; |
| 984 | + if (prop === 'width' || prop === 'height' || prop === 'left' || prop === 'top') { |
| 985 | + var numVal = val.replace(/\D+/g, ''); |
| 986 | + this[prop] = parseInt(numVal, 10); |
| 987 | + } |
928 | 988 | } |
929 | 989 | } |
930 | 990 | return this; |
|
0 commit comments