gpt4 book ai didi

javascript - 仅通过滚动更改涉及每个菜单项的背景图像

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:21 25 4
gpt4 key购买 nike

我正在开发一个 wordpress 主题,它具有通过悬停菜单项更改背景图像的功能(每个菜单项都附有不同的图像)。在移动设备上,我想通过滚动来更改背景图片,这样观众就不需要点击每个菜单来更改背景图片。

这是我实现的通过悬停改变背景的方法。 http://codepen.io/nummelin/pen/kzaso

// When any of the a's inside of sidebarContainer are hovered
$( ".sidebarContainer a" ).hover(function() {

// Removes all previous classes but keeps sidebar1
$('.sidebar1').removeClass().addClass('sidebar1');

// Splits up the URL on the current href
var URI = $(this).attr('href').split('/');
console.log(URI[2]);

// Applies the last part of the URL to sidebar1
$('.sidebar1').addClass(URI[2]);
});

通过滚动实现,我想我需要一个将菜单项悬停在其位置的功能,如下图所示。

enter image description here

有没有人知道如何实现这一目标?我一直在探索与此类似的插件或示例代码,但没有找到任何...如有任何建议,我们将不胜感激。

最佳答案

好的,这就是我写的代码。希望能帮助到你。我研究过交叉淡入淡出效果,但这并不容易。如果有人尝试过,请教我怎么做。

Codepen 上的演示: http://codepen.io/bavavava/pen/rrNpaY

jQuery(function($){
'use strict';

$(document).ready(function(){

var elem = $('li.list-item'),
$listPosition = $.map(elem, function(el, i) { return $(el).offset().top; }),
$listURL = $.map(elem, function(el,i) { return $(el).find('a').attr('href'); }),
$bg = $('.container');
console.log($listPosition);
console.log($listURL);

//PRELOAD IMAGES
$.fn.preload = function() {
this.each(function(){
$('<img/>')[0].src = this;
});
}

$($listURL).preload();

//SCROLL CHANGE
$(window).on('scroll', function () {
var windowScroll = $(this).scrollTop();

$.each($listPosition, function (i, pos) {

if (windowScroll >= pos) {
$bg.css('background', 'url(' + $listURL[i] + '), black no-repeat center center fixed');
}

});
});

});

});

关于javascript - 仅通过滚动更改涉及每个菜单项的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39180721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com