jQuery在响应断点处更改滚动图像
我有一个脚本工作正常,可以更改滚动条上的图像/徽标,但我只希望在浏览器窗口超过767px时启动此脚本,否则什么都没有/保持“原样”。如何修改此脚本以实现此目标,请执行以下操作:-jQuery在响应断点处更改滚动图像,jquery,if-statement,scroll,responsiveness,Jquery,If Statement,Scroll,Responsiveness,我有一个脚本工作正常,可以更改滚动条上的图像/徽标,但我只希望在浏览器窗口超过767px时启动此脚本,否则什么都没有/保持“原样”。如何修改此脚本以实现此目标,请执行以下操作:- jQuery(function($) { //caches a jQuery object containing the brand image element var brandimg = $(".x-brand img"); $(window).scroll(function() { var scroll = $
jQuery(function($) {
//caches a jQuery object containing the brand image element
var brandimg = $(".x-brand img");
$(window).scroll(function() {
var scroll = $(window).scrollTop();
// if( $(window).width() > 767 {
if (scroll >= 40) {
brandimg.attr('src', 'logo-icon.png');
brandimg.removeClass('x-img-1').addClass("img-2");
} else {
brandimg.attr('src', 'logo-full.png').addClass("logo-full");
brandimg.removeClass("x-img-2").addClass('img-1');
}
});
});
我不能在这里使用媒体查询,因为“Wordpress”主题使用图像而不是图像背景
不确定是否在当前函数周围有一个附加的if语句
谢谢
Glennyboy这就是我最后的做法:-
jQuery(function($) {
//caches a jQuery object containing the brand image element
var brandimg = $(".x-brand img");
jQuery(window).on("focus load resize scroll",function(e){
var scroll = $(window).scrollTop();
if (($(window).width() < 460) || (scroll >= 40)) {
brandimg.attr('src', 'logo-icon.png');
brandimg.removeClass('x-img-1').addClass("x-img-2");
} else {
brandimg.attr('src', 'logo-full.png').addClass("logo-full");
brandimg.removeClass("x-img-2").addClass('x-img-1');
}
});
});
jQuery(函数($){
//缓存包含品牌图像元素的jQuery对象
var brandimg=$(“.x-brand img”);
jQuery(窗口).on(“焦点加载调整大小滚动”,函数(e){
var scroll=$(窗口).scrollTop();
如果($(窗口).width()<460)| |(滚动>=40)){
brandimg.attr('src','logo icon.png');
brandimg.removeClass('x-img-1').addClass(“x-img-2”);
}否则{
brandimg.attr('src','logo full.png').addClass(“logo full”);
brandimg.removeClass(“x-img-2”).addClass(“x-img-1”);
}
});
});
在加载、调整大小和滚动时,它完全适用于滚动和窗口大小