简体   繁体   English

仅在使用JavaScript刷新时更改背景

[英]Change background only on refresh with javascript

Iam loading pages in dynamically with PHP like so: Iam使用PHP动态加载页面,如下所示:

<div id="content">
<div id="fadein">   


<?php
    /// load page content
    $pages_dir = 'content';

    if(!empty($_GET['p'])) {

        $pages = scandir($pages_dir, 0);
        //delete . and ..
        unset($pages[0], $pages[1]);

        $p = $_GET['p'];

        // only files that are in the array can be loaded
        if (in_array($p. '.php', $pages)) {
            include($pages_dir. '/'.$p. '.php');    

        } else {
            echo 'Helaas deze pagina bestaat niet';
        }
    } else {
        include($pages_dir. '/index.php');
    }

?>  


</div>
</div>

If you use the navigation at the top of my site the PHP loads the relevant content. 如果您使用我网站顶部的导航,则PHP将加载相关内容。

And i got this script at the bottom of my page to change the background image: 我在页面底部获得了此脚本来更改背景图片:

 function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
}

// Usage:
preload([
    '../public/background/test2/1.jpg',
    '../public/background/test2/2.jpg',
    '../public/background/test2/3.jpg',
    '../public/background/test2/4.jpg',
    '../public/background/test2/5.jpg',
    '../public/background/test2/6.jpg'

]);

var totalCount = 6;
function changeBackground()
{
    var num = Math.ceil( Math.random() * totalCount );
    backgroundUrl = '../public/background/test2/'+num+'.jpg';

    $('body').css('background-image', 'url('  +backgroundUrl+  ')');
}

changeBackground();

But everytime different content is loaded, the script is executed... I don't want that. 但是每次加载不同的内容时,都会执行脚本...我不想要那样。 I only want to change the background when the user refreshes the page. 我只想在用户刷新页面时更改背景。 Not when different content is loaded when the user is navigating through the site. 当用户在站点中导航时,如果加载了不同的内容,则不会。

As it is now (based on your comment), you are reloading the whole page when you really want to load only new content. 现在(根据您的评论),当您确实只想加载新内容时,您将重新加载整个页面。

To be able to get the effect that you want, you should use ajax (combining php and javascript) to refresh only a part of your page when you hit a navigation link. 为了获得所需的效果,当您点击导航链接时,应该使用ajax(结合php和javascript)仅刷新页面的一部分。

What you could do is (using jQuery for an easy introduction to ajax...): 您可以做的是(使用jQuery简单介绍ajax ...):

  • separate your php script from the html so that you can call it separately and include it in your initial html; 将您的php脚本与html分开,以便您可以分别调用它并将其包含在初始html中;
  • attach an event handler to your navigation links, cancelling the default action ( undo the click) and use jQuery's load method to replace your content with the new content (call your php script with the correct parameters). 将事件处理程序附加到导航链接,取消默认操作( 取消单击),然后使用jQuery的load方法将您的内容替换为新内容(使用正确的参数调用php脚本)。

As the javascript is located in the main html file, it will only execute your background script on the initial page load or page refresh. 由于javascript位于主html文件中,因此它将仅在初始页面加载或页面刷新时执行后台脚本。

如果您尝试仅通过JavaScript来管理此问题,则可以创建一个Cookie,该Cookie包含一个页面访问数组,然后在页面加载时可以引用该数组来确定用户是否已经访问了该页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM