Hello. Recently I received a code, it works on the site, but for some reason when I put it on my site it no longer works. Please look can find something. I'm breaking my head for the second day.

html code:

var gallery = { cnt: 0, container: document.getElementById("container_image"), timer: null, rds: "btn1", pos: 0, top: 1, init: function(i) { this.cnt = i; this.container.style.width = i * 700 + "px"; this.timer = setInterval(function() { gallery.start(); }, 1500); }, start: function() { if (this.top == 1) { this.pos = 0; this.top += 1; document.getElementById("btn1").style.backgroundColor = "red"; document.getElementById("btn2").style.backgroundColor = "green"; document.getElementById("btn3").style.backgroundColor = "green"; } else if (this.top == 2) { this.pos -= 350 * 2; this.top += 1; document.getElementById("btn1").style.backgroundColor = "green"; document.getElementById("btn2").style.backgroundColor = "red"; document.getElementById("btn3").style.backgroundColor = "green"; } else if (this.top == 3) { this.pos -= 350 * 2; this.top = 1; document.getElementById("btn1").style.backgroundColor = "green"; document.getElementById("btn2").style.backgroundColor = "green"; document.getElementById("btn3").style.backgroundColor = "red"; } this.container.style.left = this.pos + "px"; }, slide: function(event) { var e = event || window.event; var target = e.target; if (target.tagName.toLowerCase() != "span") return; var id_click = target.id; var pos = document.getElementById(id_click).offsetLeft; if (id_click == "btn1") pos = 0; else if (id_click == "btn2") pos -= 526 * 2; else if (id_click == "btn3") pos -= 593 * 3; else pos = 247; this.container.style.left = pos + "px"; } } 
 #container { position: absolute; margin: 0 auto; width: 780px; height: 280px; padding-left: 160px; } .prev { float: left; width: 5%; height: 50%; background: red; cursor: pointer; } .photo { float: left; width: 780px; height: 280px; background: yellow; overflow: hidden; } .next { float: left; width: 5%; height: 50%; background: red; cursor: pointer; } .prev:hover, .next:hover { background: green; } .container-photo { width: 780px; height: 280px; -webkit-transition: all 1s; transition: all 1s; position: relative; } .image-block { width: 780px; height: 280px; float: left; background: pink; z-index: 1; } .clear { clear: both; } .rds { padding: 10px; line-height: 150%; width: 100%; text-align: center; } .rds-bl { width: 15px; height: 15px; border-radius: 50%; cursor: pointer; margin: 4px; background: green; display: inline-block; } 
 <!DOCTYPE HTML> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <title>--------------------</title> <meta charset="utf-8"> <link rel="icon" href="images/icon.png" type="image/x-icon"> <link rel="stylesheet" type="text/css" href="container/swap.css"> <script type="text/javascript" src="container/swap.js"></script> <script> window.onload = function() { gallery.init(3); } </script> </head> <body> <div id="container"> <div class="photo"> <div class="container-photo" id="container_image"> <div class="image-block" id="image_block"> <span>Здесь может быть image, ссылки и прочее #1</span> </div> <div class="image-block"> <span>Здесь может быть image, ссылки и прочее #2</span> </div> <div class="image-block"> <span>Здесь может быть image, ссылки и прочее #3</span> </div> <div class="clear"></div> </div> </div> <div class="clear"></div> <div class="rds" onclick="gallery.slide(event)"> <span class="rds-bl" id="btn1"></span> <span class="rds-bl" id="btn2"></span> <span class="rds-bl" id="btn3"></span> </div> </div> </body> </html> 

Here is a screen from the site: enter image description here

But nothing revolves on the site, nothing happens. If you expand the block, it turns out that all the display blocks are built vertically.

Closed due to the fact that it was off topic by aleksandr barakin , Denis , user194374, cheops , Regent Jul 16, '16 at 7:46 pm .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - aleksandr barakin, Denis, Community Spirit, cheops, Regent
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    my not panimat ¯ \ _ (ツ) _ / ¯ - Denis
  • connect the script at the bottom of the page ... it does not have time to load ... - C.Raf.T
  • @CostaRaf didn’t quite understand where and how to connect the script in which block from below - Sambl
  • @CostaRaf, the matter is not in the place where the script is connected, the onload event is on it, which means that you can connect it at the very beginning - Mr. Black
  • @Doofy but you don’t know why it doesn’t work on my site, but everything works fine here? - Sambl

1 answer 1

When you connect <script> , then connect it to the bottom (after) the html code. as in the example below:

 <!DOCTYPE HTML> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <title>--------------------</title> <meta charset="utf-8"> <link rel="icon" href="images/icon.png" type="image/x-icon"> <link rel="stylesheet" type="text/css" href="container/swap.css"> <script type="text/javascript" src="container/swap.js"></script> </head> <body> <style> #container { position: absolute; margin: 0 auto; width: 780px; height: 280px; padding-left: 160px; } .prev { float: left; width: 5%; height: 50%; background: red; cursor: pointer; } .photo { float: left; width: 780px; height: 280px; background: yellow; overflow: hidden; } .next { float: left; width: 5%; height: 50%; background: red; cursor: pointer; } .prev:hover, .next:hover { background: green; } .container-photo { width: 780px; height: 280px; -webkit-transition: all 1s; transition: all 1s; position: relative; } .image-block { width: 780px; height: 280px; float: left; background: pink; z-index: 1; } .clear { clear: both; } .rds { padding: 10px; line-height: 150%; width: 100%; text-align: center; } .rds-bl { width: 15px; height: 15px; border-radius: 50%; cursor: pointer; margin: 4px; background: green; display: inline-block; } </style> <div id="container"> <div class="photo"> <div class="container-photo" id="container_image"> <div class="image-block" id="image_block"> <span>Здесь может быть image, ссылки и прочее #1</span> </div> <div class="image-block"> <span>Здесь может быть image, ссылки и прочее #2</span> </div> <div class="image-block"> <span>Здесь может быть image, ссылки и прочее #3</span> </div> <div class="clear"></div> </div> </div> <div class="clear"></div> <div class="rds" onclick="gallery.slide(event)"> <span class="rds-bl" id="btn1"></span> <span class="rds-bl" id="btn2"></span> <span class="rds-bl" id="btn3"></span> </div> </div> <script> window.onload = function() { gallery.init(3); } var gallery = { cnt: 0, container: document.getElementById("container_image"), timer: null, rds: "btn1", pos: 0, top: 1, init: function(i) { this.cnt = i; this.container.style.width = i * 700 + "px"; this.timer = setInterval(function() { gallery.start(); }, 1500); }, start: function() { if (this.top == 1) { this.pos = 0; this.top += 1; document.getElementById("btn1").style.backgroundColor = "red"; document.getElementById("btn2").style.backgroundColor = "green"; document.getElementById("btn3").style.backgroundColor = "green"; } else if (this.top == 2) { this.pos -= 350 * 2; this.top += 1; document.getElementById("btn1").style.backgroundColor = "green"; document.getElementById("btn2").style.backgroundColor = "red"; document.getElementById("btn3").style.backgroundColor = "green"; } else if (this.top == 3) { this.pos -= 350 * 2; this.top = 1; document.getElementById("btn1").style.backgroundColor = "green"; document.getElementById("btn2").style.backgroundColor = "green"; document.getElementById("btn3").style.backgroundColor = "red"; } this.container.style.left = this.pos + "px"; }, slide: function(event) { var e = event || window.event; var target = e.target; if (target.tagName.toLowerCase() != "span") return; var id_click = target.id; var pos = document.getElementById(id_click).offsetLeft; if (id_click == "btn1") pos = 0; else if (id_click == "btn2") pos -= 526 * 2; else if (id_click == "btn3") pos -= 593 * 3; else pos = 247; this.container.style.left = pos + "px"; } } </script> </body> </html> 

Or you can move the entire js to a separate file and connect it to the external path like this:

 <script type="text/javascript" src="foo.js"></script> </body> </html> 
  • Thank you very much, you really helped, I thought it does not matter where to connect. - Sambl