Javascript-форум (https://javascript.ru/forum/)
-   Ваши сайты и скрипты (https://javascript.ru/forum/project/)
-   -   Очерёдность выполнения переменных (https://javascript.ru/forum/project/53019-ocherjodnost-vypolneniya-peremennykh.html)

Сергей89 17.01.2015 22:15

Очерёдность выполнения переменных
 
Как сделать чтобы сначала выполнялась var mql, а потом var img?

<!-- xmas_lights [ start ] -->
<script type="text/javascript">
document.write("<div id='lights' style='position:absolute;width:956px;height:180px;top:-20px;background:url(<?php echo plugins_url() . '/xmas-lights/'; ?>light.png) no-repeat;left:50%;margin-left:-478px'></div>")

setInterval( "xmasLights();", 1000 );
var lights = "active";
function xmasLights(){
	var img = document.getElementById('lights');
	if(lights == "active"){
		img.style.backgroundPosition = "0 -180px";
		lights = "inactive";
	}else{
		img.style.backgroundPosition = "";
		lights = "active";
  	}
        var mql = window.matchMedia('all and (max-width: 1000px)');
             if (mql.matches){
                 img.style.display = "none";
         }else {
                  img.style.display = "block";
               } 
}
</script>
<!-- xmas_lights [ end ] -->

Safort 17.01.2015 22:28

Поменяй их местами.

danik.js 17.01.2015 22:34

А зачем там вообще document.write? Или скрипт не твой? setInterval и без строки работает хорошо. getElementById не нужно вызывать циклически. Скрытие на узких экранах лучше делать через css. Также как и смещение background-position.


Часовой пояс GMT +3, время: 16:18.