{"id":2107,"date":"2020-09-10T11:03:47","date_gmt":"2020-09-10T09:03:47","guid":{"rendered":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2107"},"modified":"2020-09-11T05:43:10","modified_gmt":"2020-09-11T03:43:10","slug":"automatically-alternate-a-div-in-a-web-page","status":"publish","type":"post","link":"https:\/\/digitalia.culturanuova.net\/blog\/?p=2107","title":{"rendered":"automatically alternate a div in a web page"},"content":{"rendered":"\n<p>It is quite easy automatically alternate a (<strong>image<\/strong>) banner in a webpage with javascript. But if you want alternate automatically <strong>a complex text<\/strong> it is not so easy.<\/p>\n\n\n\n<p>First of all you have to know that is <strong>not<\/strong> possible use <strong>php<\/strong>, because the &#8220;duty&#8221; of php ends with the &#8220;production&#8221; of a page, and it is not usable to change something in a page alrealdy &#8220;made&#8221;.<\/p>\n\n\n\n<p>Therefore, the only way is javascript. I found a js to change automatically images (every x time) and I modified it according to my needs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">il javascript<\/h4>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p><code>var slideIndex = 0;<br>showSlides();<\/code><\/p>\n\n\n\n<p><code>function showSlides() {<br>var i;<br>var slides = document.getElementsByClassName(\"mySlides\");<br>var dots = document.getElementsByClassName(\"dot\");<br>for (i = 0; i &lt; slides.length; i++) { slides[i].style.display = \"none\"; } slideIndex++; if (slideIndex &gt; slides.length) {slideIndex = 1}<br>for (i = 0; i &lt; dots.length; i++) {<br>dots[i].className = dots[i].className.replace(\" active\", \"\");<br>}<br>\/\/ slides[slideIndex-1].style.display = \"block\";<br>\/\/ slides[slideIndex-1].className = slides[i].className.replace(\" visible\", \"\");<br>slides[slideIndex-1].style.display = \"flex\";<br>dots[slideIndex-1].className += \" active\";<br>setTimeout(showSlides, 4000); \/\/ Change image every 4 seconds<br>}<\/code><\/p>\n<\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">l&#8217;html<\/h4>\n\n\n\n<p>Afterward I was able to use, within the html proposed a php code, as in the following<\/p>\n\n\n\n<pre style=\"font-family:arial;font-size:12px;border:1px dashed #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;background-image:URL(http:\/\/2.bp.blogspot.com\/_z5ltvMQPaa8\/SjJXr_U2YBI\/AAAAAAAAAAM\/46OqEP32CJ8\/s320\/codebg.gif);padding:0px;color:#000000;text-align:left;line-height:20px;\"><code style=\"color:#000000;word-wrap:normal;\"> $ads_verticale_dx=\"&lt;code&gt;&lt;p class=\\\"mySlides fade\\\"&gt;&lt;span class=\\\"numbertext\\\"&gt;1 \/ 3&lt;\/span&gt;&lt;img src=\\\"someimage.jpg\\\" width=\\\"100px\\\" \/&gt;&lt;br \/&gt;some &lt;b&gt;text&lt;\/b&gt; with&lt;br \/&gt; &lt;a rel=\\\"external\\\" href=\\\"somelink\/\\\"&gt;some link&lt;\/a&gt;&lt;\/p&gt;  \n &lt;p class=\\\"mySlides fade\\\"&gt;&lt;span class=\\\"numbertext\\\"&gt;2 \/ 3&lt;\/span&gt;&lt;img src=\\\"someotherimage.jpg\\\" width=\\\"100px\\\" \/&gt;&lt;br \/&gt;some other &lt;b&gt;text&lt;\/b&gt; with a&lt;br \/&gt;  \n &lt;a rel=\\\"external\\\" href=\\\"somelink\/\\\"&gt;some other link&lt;\/a&gt;&lt;\/p&gt;  \n &lt;div style=\\\"text-align:center\\\"&gt;&lt;span class=\\\"dot\\\"&gt;&lt;\/span&gt;&lt;span class=\\\"dot\\\"&gt;&lt;\/span&gt;&lt;span class=\\\"dot\\\"&gt;&lt;\/span&gt;&lt;\/div&gt;  \n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">il css<\/h4>\n\n\n\n<p>And this is the css to add<\/p>\n\n\n\n<code><pre>.mySlides {display: none; \n  }\n\n\/* Slideshow container *\/\n.slideshow-container { vertical-align: middle; \n}\n\n\/* Caption text *\/\n.text {\n  display: none;\n  color: #f2f2f2;\n  font-size: 15px;\n  padding: 8px 12px;\n  width: 100%;\n  text-align: center;\n}\n\n\/* Number text (1\/3 etc) *\/\n.numbertext {\n  visibility: hidden;\n  color: #f2f2f2;\n  font-size: 12px;\n  padding: 8px 12px;\n  top: 0;\n}\n\n\/* The dots\/bullets\/indicators *\/\n.dot {\n  height: 15px;\n  width: 15px;\n  margin: 0 2px;\n  background-color: #bbb;\n  border-radius: 50%;\n  display: none;\n  transition: background-color 0.6s ease;\n}\n\n.active {\n  background-color: #717171;\n}\n\n\/* Fading animation *\/\n.fade {\n  -webkit-animation-name: fade;\n  -webkit-animation-duration: 1.5s;\n  animation-name: fade;\n  animation-duration: 1.5s;\n}\n\n@-webkit-keyframes fade {\n  from {opacity: .4} \n  to {opacity: 1}\n}\n\n@keyframes fade {\n  from {opacity: .4} \n  to {opacity: 1}\n}\n\n\/* On smaller screens, decrease text size *\/\n@media only screen and (max-width: 300px) {\n  .text {font-size: 11px}\n}<\/pre><\/code>\n\n\n\n<p>Last problem: how <strong>load<\/strong> the javascript. You can put it within the <em>body<\/em>, and this way it works without proiblems, but is a quite ugly solution. The best way is tu put the script within the <em>head<\/em>, or in a linked js file with all the other scripts, and then do (wihin the body tag) the command onLoad=&#8221;showSlides();&#8221;. But doing so, some browser, such as <strong>Chrome<\/strong>, could not understand the command. To fix this issue, be sure to use the correct <em>case<\/em> (onLoad, not onload) and try to delete the browser <em>cache<\/em>. This way you should force Chrome to recognize the script.<\/p>\n\n\n\n<p>It works!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is quite easy automatically alternate a (image) banner in a webpage with javascript. But if you want alternate automatically a complex text it is not so easy. First of all you have to know that is not possible use php, because the &#8220;duty&#8221; of php ends with the &#8220;production&#8221; of a page, and it &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/digitalia.culturanuova.net\/blog\/?p=2107\" class=\"more-link\">Leggi tutto<span class=\"screen-reader-text\"> &#8220;automatically alternate a div in a web page&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[566],"tags":[614,616,615,249,617],"class_list":["post-2107","post","type-post","status-publish","format-standard","hentry","category-webmaster","tag-alternate-div","tag-automatic","tag-automatically","tag-chrome","tag-onload"],"_links":{"self":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2107"}],"version-history":[{"count":9,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2107\/revisions"}],"predecessor-version":[{"id":2116,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2107\/revisions\/2116"}],"wp:attachment":[{"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digitalia.culturanuova.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}