简体   繁体   English

使用jQuery平稳地更改每个导航菜单的站点背景图像

[英]Change background image of the site for each navigation menu smoothly using jquery

I use javascript function for each menu and call that function onClick event. 我为每个菜单使用javascript函数,并调用该函数的onClick事件。 What i face is the image is not changing smoothly, Some times its loading slowly. 我面对的是图像无法平稳变化,有时加载缓慢。

Lets consider 2 links each link calls a function for changing the background of the body 让我们考虑2个链接,每个链接调用一个用于更改主体背景的函数

This is how i change now 这就是我现在改变的方式

function events(){
   $("body").css('background-image','url(./wp-content/Uploads/img3.jpg)');
}

function projects(){
   $("body").css('background-image','url(./wp-content/Uploads/img2.jpg)');
   return false;
}

HTML HTML

<li id="projectsm"><a href="#projects" onClick="projects();" class="stylish">Projects</a></li>
<li id="eventsm"><a href="#events" onClick="events();" class="stylish">Events</a></li>

So when i click the menu it must transform like fade out or something smoothly not like flashing 因此,当我单击菜单时,它必须像淡入淡出或平滑地闪烁而不是闪烁

If I understand your problem, you can change body background image with jQuery on click on a link, but it's not smooth / fast enough . 如果我理解您的问题,则可以在单击链接时使用jQuery更改主体背景图像,但是它不够平滑/不够快

I think it's mainly because when you change your background, the new background isn't loaded yet. 我认为主要是因为当您更改背景时,尚未加载新背景。 So it have to load, then to change when it's loaded. 因此,它必须先加载,然后在加载时进行更改。

Solutions : 解决方案:

  • you can load all the backgrounds on document load 您可以加载文档时加载所有背景

  • you can load the new background and then call your switch function 您可以加载新的背景,然后调用您的切换功能

In both case, you can use ImagesLoaded plugin to load your images when you want ( https://github.com/desandro/imagesloaded ). 在这两种情况下,您都可以在需要时使用ImagesLoaded插件加载图像( https://github.com/desandro/imagesloaded )。

Then if you want a smooth transition , you can use CSS3 Transition on your body selector : 然后,如果要平滑过渡 ,可以在身体选择器上使用CSS3 Transition:

transition: background-image 1s ease-in-out;

But I'm not sure transition works on background-image property... 但是我不确定过渡是否可以在背景图像属性上进行...

Try this code, if that's what you want: 如果需要的话,请尝试以下代码:

You need load image first, then use javascript. 您需要先加载图片,然后再使用javascript。

HTML: HTML:

<img src="http://lorempixel.com/400/200/city" alt="" width="0" height="0" />
<img src="http://lorempixel.com/400/200/people" alt="" width="0" height="0" />
<ul>
    <li id="projectsm">Projects</li>
    <li id="eventsm">Events</li>
</ul>

JS: JS:

$(function(){
    $('#projectsm').click(function(){
        $('body').css('background-image','url(http://lorempixel.com/400/200/city)')
    });
    $('#eventsm').click(function(){
        $('body').css('background-image','url(http://lorempixel.com/400/200/people)')
    });
});

and for smooth transition, use css3 transition property 为了平稳过渡,请使用css3过渡属性

CSS: CSS:

body{
    background-image:url(http://lorempixel.com/400/200/animals);
    background-repeat:no-repeat;
    background-size:cover;
    -webkit-transition: background 1s ease-in-out;
    -moz-transition: background 1s ease-in-out;
    -ms-transition: background 1s ease-in-out;
    -o-transition: background 1s ease-in-out;
    transition: background 1s ease-in-out;
}

http://jsfiddle.net/sL8q7nbb/1/ http://jsfiddle.net/sL8q7nbb/1/

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

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