|
924 | 924 | */
|
925 | 925 | function createBackground( slide, container ) {
|
926 | 926 |
|
| 927 | + |
| 928 | + // Main slide background element |
| 929 | + var element = document.createElement( 'div' ); |
| 930 | + element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' ); |
| 931 | + |
| 932 | + // Inner background element that wraps images/videos/iframes |
| 933 | + var contentElement = document.createElement( 'div' ); |
| 934 | + contentElement.className = 'slide-background-content'; |
| 935 | + |
| 936 | + element.appendChild( contentElement ); |
| 937 | + container.appendChild( element ); |
| 938 | + |
| 939 | + slide.slideBackgroundElement = element; |
| 940 | + slide.slideBackgroundContentElement = contentElement; |
| 941 | + |
| 942 | + // Syncs the background to reflect all current background settings |
| 943 | + syncBackground( slide ); |
| 944 | + |
| 945 | + return element; |
| 946 | + |
| 947 | + } |
| 948 | + |
| 949 | + /** |
| 950 | + * Renders all of the visual properties of a slide background |
| 951 | + * based on the various background attributes. |
| 952 | + * |
| 953 | + * @param {HTMLElement} slide |
| 954 | + */ |
| 955 | + function syncBackground( slide ) { |
| 956 | + |
| 957 | + var element = slide.slideBackgroundElement, |
| 958 | + contentElement = slide.slideBackgroundContentElement; |
| 959 | + |
| 960 | + // Reset the prior background state in case this is not the |
| 961 | + // initial sync |
| 962 | + slide.classList.remove( 'has-dark-background' ); |
| 963 | + slide.classList.remove( 'has-light-background' ); |
| 964 | + |
| 965 | + element.removeAttribute( 'data-loaded' ); |
| 966 | + element.removeAttribute( 'data-background-hash' ); |
| 967 | + element.removeAttribute( 'data-background-size' ); |
| 968 | + element.removeAttribute( 'data-background-transition' ); |
| 969 | + element.style.backgroundColor = ''; |
| 970 | + |
| 971 | + contentElement.style.backgroundSize = ''; |
| 972 | + contentElement.style.backgroundRepeat = ''; |
| 973 | + contentElement.style.backgroundPosition = ''; |
| 974 | + contentElement.style.backgroundImage = ''; |
| 975 | + contentElement.style.opacity = ''; |
| 976 | + contentElement.innerHTML = ''; |
| 977 | + |
927 | 978 | var data = {
|
928 | 979 | background: slide.getAttribute( 'data-background' ),
|
929 | 980 | backgroundSize: slide.getAttribute( 'data-background-size' ),
|
|
937 | 988 | backgroundOpacity: slide.getAttribute( 'data-background-opacity' )
|
938 | 989 | };
|
939 | 990 |
|
940 |
| - // Main slide background element |
941 |
| - var element = document.createElement( 'div' ); |
942 |
| - element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' ); |
943 |
| - |
944 |
| - // Inner background element that wraps images/videos/iframes |
945 |
| - var contentElement = document.createElement( 'div' ); |
946 |
| - contentElement.className = 'slide-background-content'; |
947 |
| - |
948 | 991 | if( data.background ) {
|
949 | 992 | // Auto-wrap image urls in url(...)
|
950 | 993 | if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)([?#\s]|$)/gi.test( data.background ) ) {
|
|
982 | 1025 | if( data.backgroundPosition ) contentElement.style.backgroundPosition = data.backgroundPosition;
|
983 | 1026 | if( data.backgroundOpacity ) contentElement.style.opacity = data.backgroundOpacity;
|
984 | 1027 |
|
985 |
| - element.appendChild( contentElement ); |
986 |
| - container.appendChild( element ); |
987 |
| - |
988 |
| - // If backgrounds are being recreated, clear old classes |
989 |
| - slide.classList.remove( 'has-dark-background' ); |
990 |
| - slide.classList.remove( 'has-light-background' ); |
991 |
| - |
992 |
| - slide.slideBackgroundElement = element; |
993 |
| - slide.slideBackgroundContentElement = contentElement; |
994 |
| - |
995 | 1028 | // If this slide has a background color, add a class that
|
996 | 1029 | // signals if it is light or dark. If the slide has no background
|
997 | 1030 | // color, no class will be set
|
|
1012 | 1045 | }
|
1013 | 1046 | }
|
1014 | 1047 |
|
1015 |
| - return element; |
1016 |
| - |
1017 | 1048 | }
|
1018 | 1049 |
|
1019 | 1050 | /**
|
|
2638 | 2669 |
|
2639 | 2670 | }
|
2640 | 2671 |
|
| 2672 | + /** |
| 2673 | + * Updates reveal.js to keep in sync with new slide attributes. For |
| 2674 | + * example, if you add a new `data-background-image` you can call |
| 2675 | + * this to have reveal.js render the new background image. |
| 2676 | + * |
| 2677 | + * Similar to #sync() but more efficient when you only need to |
| 2678 | + * refresh a specific slide. |
| 2679 | + * |
| 2680 | + * @param {HTMLElement} slide |
| 2681 | + */ |
| 2682 | + function syncSlide( slide ) { |
| 2683 | + |
| 2684 | + syncBackground( slide ); |
| 2685 | + |
| 2686 | + sortFragments( slide.querySelectorAll( '.fragment' ) ); |
| 2687 | + |
| 2688 | + updateBackground(); |
| 2689 | + updateNotes(); |
| 2690 | + |
| 2691 | + loadSlide( slide ); |
| 2692 | + |
| 2693 | + } |
| 2694 | + |
2641 | 2695 | /**
|
2642 | 2696 | * Resets all vertical slides so that only the first
|
2643 | 2697 | * is visible.
|
|
5233 | 5287 | initialize: initialize,
|
5234 | 5288 | configure: configure,
|
5235 | 5289 | sync: sync,
|
| 5290 | + syncSlide: syncSlide, |
5236 | 5291 |
|
5237 | 5292 | // Navigation methods
|
5238 | 5293 | slide: slide,
|
|
0 commit comments