gpt4 book ai didi

javascript - Firefox 中的 div-background-switcher 偶尔出错

转载 作者:太空宇宙 更新时间:2023-11-04 05:59:56 25 4
gpt4 key购买 nike

所以我有一个 div,我可以在其中使用脚本更改背景图像。我在这里简化了一切:https://inmeditas.satsang-hamburg.de/test.html不幸的是,大约有 10% 的时间我在 Firefox 中遇到如下错误:error in Firefox到目前为止,一切似乎在 Chrome 和其他浏览器中都运行良好。这是代码片段:

/*!
* jQuery.BgSwitcher
*
* @version 0.4.3
* @author rewish <rewish.org@gmail.com>
* @license MIT License (https://github.com/rewish/jquery-bgswitcher/blob/master/LICENSE.md)
* @link https://github.com/rewish/jquery-bgswitcher
*/
(function($) {
'use strict';

var loadedImages = {},

slice = Array.prototype.slice,
toString = Object.prototype.toString,

corners = ['Top', 'Right', 'Bottom', 'Left'],
backgroundProperties = [
'Attachment', 'Color', 'Image', 'Repeat',
'Position', 'Size', 'Clip', 'Origin'
];

$.fn.bgswitcher = function() {
var args = arguments,
instanceKey = BgSwitcher.keys.instance;

return this.each(function() {
var instance = $.data(this, instanceKey);

if (!instance) {
instance = new BgSwitcher(this);
$.data(this, instanceKey, instance);
}

instance.dispatch.apply(instance, args);
});
};

// Backward Compatibility
$.fn.bgSwitcher = $.fn.bgswitcher;

/**
* BgSwitcher
*
* @param {HTMLElement} el
* @constructor
*/
function BgSwitcher(el) {
this.$el = $(el);
this.index = 0;
this.config = $.extend({}, BgSwitcher.defaultConfig);

this._setupBackgroundElement();
this._listenToResize();
}

$.extend(BgSwitcher.prototype, {
/**
* Dispatch
*
* @param {string|Array} one
*/
dispatch: function(one) {
switch (toString.call(one)) {
case '[object Object]':
this.setConfig(one);
break;
case '[object String]':
this[one].apply(this, slice.call(arguments, 1));
break;
default:
throw new Error('Please specify a Object or String');
}
},

/**
* Set config
*
* @param {Object} config
*/
setConfig: function(config) {
this.config = $.extend(this.config, config);

if (typeof this.config.random !== 'undefined') {
this.config.shuffle = this.config.random;
}

this.refresh();
},

/**
* Set images
*
* @param {Array} images
*/
setImages: function(images) {
this.imageList = new this.constructor.ImageList(images);

if (this.config.shuffle) {
this.imageList.shuffle();
}
},

/**
* Set switch handler
*
* @param {Function} fn
*/
setSwitchHandler: function(fn) {
this.switchHandler = $.proxy(fn, this);
},

/**
* Default switch handler
*
* @param {string} type
* @returns {Function}
*/
getBuiltInSwitchHandler: function(type) {
return this.constructor.switchHandlers[type || this.config.effect];
},

/**
* Refresh
*/
refresh: function() {
this.setImages(this.config.images);
this.setSwitchHandler(this.getBuiltInSwitchHandler());
this._prepareSwitching();

if (this.config.start) {
this.start();
}
},

/**
* Start switching
*/
start: function() {
if (!this._timerID) {
this._timerID = setTimeout($.proxy(this, 'next'), this.config.interval);
}
},

/**
* Stop switching
*/
stop: function() {
if (this._timerID) {
clearTimeout(this._timerID);
this._timerID = null;
}
},

/**
* Toggle between start/stop
*/
toggle: function() {
if (this._timerID) {
this.stop();
} else {
this.start();
}
},

/**
* Reset switching
*/
reset: function() {
this.index = 0;
this._prepareSwitching();
},

/**
* Go to next switching
*/
next: function() {
var max = this.imageList.count();

if (!this.config.loop && this.index + 1 === max) {
return;
}

if (++this.index === max) {
this.index = 0;
}

this.switching();
},

/**
* Go to previous switching
*/
prev: function() {
if (!this.config.loop && this.index === 0) {
return;
}

if (--this.index === -1) {
this.index = this.imageList.count() - 1;
}

this.switching();
},

/**
* Select the switching at index
*
* @param {number} index
*/
select: function(index) {
if (index === -1) {
index = this.imageList.count() - 1;
}

this.index = index;
this.switching();
},

/**
* Switching the background image
*/
switching: function() {
var started = !!this._timerID;

if (started) {
this.stop();
}

this._createSwitchableElement();
this._prepareSwitching();
this.switchHandler(this.$switchable);

if (started) {
this.start();
}
},

/**
* Destroy...
*/
destroy: function() {
this.stop();
this._stopListeningToResize();

if (this.$switchable) {
this.$switchable.stop();
this.$switchable.remove();
this.$switchable = null;
}

if (this.$bg) {
this.$bg.remove();
this.$bg = null;
}

this.$el.removeAttr('style');
this.$el.removeData(this.constructor.keys.instance);
this.$el = null;
},

/**
* Adjust rectangle
*/
_adjustRectangle: function() {
var corner,
i = 0,
length = corners.length,
offset = this.$el.position(),
copiedStyles = {
top: offset.top,
left: offset.left,
width: this.$el.innerWidth(),
height: this.$el.innerHeight()
};

for (; i < length; i++) {
corner = corners[i];
copiedStyles['margin' + corner] = this.$el.css('margin' + corner);
copiedStyles['border' + corner] = this.$el.css('border' + corner);
}

this.$bg.css(copiedStyles);
},

/**
* Setup background element
*/
_setupBackgroundElement: function() {
this.$bg = $(document.createElement('div'));
this.$bg.css({
position: 'absolute',
zIndex: (parseInt(this.$el.css('zIndex'), 10) || 0) - 1,
overflow: 'hidden'
});

this._copyBackgroundStyles();
this._adjustRectangle();

if (this.$el[0].tagName === 'BODY') {
this.$el.prepend(this.$bg);
} else {
this.$el.before(this.$bg);
this.$el.css('background', 'none');
}
},

/**
* Create switchable element
*/
_createSwitchableElement: function() {
if (this.$switchable) {
this.$switchable.remove();
}

this.$switchable = this.$bg.clone();
this.$switchable.css({top: 0, left: 0, margin: 0, border: 'none'});
this.$switchable.appendTo(this.$bg);
},

/**
* Copy background styles
*/
_copyBackgroundStyles: function () {
var prop,
copiedStyle = {},
i = 0,
length = backgroundProperties.length,
backgroundPosition = 'backgroundPosition';

for (; i < length; i++) {
prop = 'background' + backgroundProperties[i];
copiedStyle[prop] = this.$el.css(prop);
}

// For IE<=9
if (copiedStyle[backgroundPosition] === undefined) {
copiedStyle[backgroundPosition] = [
this.$el.css(backgroundPosition + 'X'),
this.$el.css(backgroundPosition + 'Y')
].join(' ');
}

this.$bg.css(copiedStyle);
},

/**
* Listen to the resize event
*/
_listenToResize: function() {
var that = this;
this._resizeHandler = function() {
that._adjustRectangle();
};
$(window).on('resize', this._resizeHandler);
},

/**
* Stop listening to the resize event
*/
_stopListeningToResize: function() {
$(window).off('resize', this._resizeHandler);
this._resizeHandler = null;
},

/**
* Prepare the Switching
*/
_prepareSwitching: function() {
this.$bg.css('backgroundImage', this.imageList.url(this.index));
}
});

/**
* Data Keys
* @type {Object}
*/
BgSwitcher.keys = {
instance: 'bgSwitcher'
};

/**
* Default Config
* @type {Object}
*/
BgSwitcher.defaultConfig = {
images: [],
interval: 5000,
start: true,
loop: true,
shuffle: false,
effect: 'fade',
duration: 1000,
easing: 'swing'
};

/**
* Built-In switch handlers (effects)
* @type {Object}
*/
BgSwitcher.switchHandlers = {
fade: function($el) {
$el.animate({opacity: 0}, this.config.duration, this.config.easing);
},

blind: function($el) {
$el.animate({height: 0}, this.config.duration, this.config.easing);
},

clip: function($el) {
$el.animate({
top: parseInt($el.css('top'), 10) + $el.height() / 2,
height: 0
}, this.config.duration, this.config.easing);
},

slide: function($el) {
$el.animate({top: -$el.height()}, this.config.duration, this.config.easing);
},

drop: function($el) {
$el.animate({
left: -$el.width(),
opacity: 0
}, this.config.duration, this.config.easing);
},

hide: function($el) {
$el.hide();
}
};

/**
* Define effect
*
* @param {String} name
* @param {Function} fn
*/
BgSwitcher.defineEffect = function(name, fn) {
this.switchHandlers[name] = fn;
};

/**
* BgSwitcher.ImageList
*
* @param {Array} images
* @constructor
*/
BgSwitcher.ImageList = function(images) {
this.images = images;
this.createImagesBySequence();
this.preload();
};

$.extend(BgSwitcher.ImageList.prototype, {
/**
* Images is sequenceable
*
* @returns {boolean}
*/
isSequenceable: function() {
return typeof this.images[0] === 'string' &&
typeof this.images[1] === 'number' &&
typeof this.images[2] === 'number';
},

/**
* Create an images by sequence
*/
createImagesBySequence: function() {
if (!this.isSequenceable()) {
return;
}

var images = [],
base = this.images[0],
min = this.images[1],
max = this.images[2];

do {
images.push(base.replace(/\.\w+$/, min + '$&'));
} while (++min <= max);

this.images = images;
},

/**
* Preload an images
*/
preload: function() {
var path,
length = this.images.length,
i = 0;

for (; i < length; i++) {
path = this.images[i];
if (!loadedImages[path]) {
loadedImages[path] = new Image();
loadedImages[path].src = path;
}
}
},

/**
* Shuffle an images
*/
shuffle: function() {
var j, t,
i = this.images.length,
original = this.images.join();

if (!i) {
return;
}

while (i) {
j = Math.floor(Math.random() * i);
t = this.images[--i];
this.images[i] = this.images[j];
this.images[j] = t;
}

if (this.images.join() === original) {
this.shuffle();
}
},

/**
* Get the image from index
*
* @param {number} index
* @returns {string}
*/
get: function(index) {
return this.images[index];
},

/**
* Get the URL with function of CSS
*
* @param {number} index
* @returns {string}
*/
url: function(index) {
return 'url(' + this.get(index) + ')';
},

/**
* Count of images
*
* @returns {number}
*/
count: function() {
return this.images.length;
}
});

$.BgSwitcher = BgSwitcher;
}(jQuery));


$(".amrum").bgswitcher({
images: ["https://inmeditas.satsang-hamburg.de/headerAmrum1.jpg", "https://inmeditas.satsang-hamburg.de/headerAmrum2.jpg", "https://inmeditas.satsang-hamburg.de/headerAmrum3.jpg", "https://inmeditas.satsang-hamburg.de/headerAmrum4.jpg"],
interval: 5000,
duration: 1000
});
.amrum {
background-position: center;
background-size: cover;
}
.unterseite {
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="amrum"><img class="unterseite" src="https://inmeditas.satsang-hamburg.de/headerAmrum-hilf.png" /></div>

我希望有一个没有这个错误并且没有 Bootstrap 4 Navbar 问题的解决方案(不幸的是,我尝试过其他一些脚本)。

感谢您的帮助。

最佳答案

我现在使用 http://responsiveslides.com/到目前为止,它适用于 Bootstrap 4,我没有收到任何错误。

/*! ResponsiveSlides.js v1.55
* http://responsiveslides.com
* http://viljamis.com
*
* Copyright (c) 2011-2012 @viljamis
* Available under the MIT license
*/

/*jslint browser: true, sloppy: true, vars: true, plusplus: true, indent: 2 */

(function ($, window, i) {
$.fn.responsiveSlides = function (options) {

// Default settings
var settings = $.extend({
"auto": true, // Boolean: Animate automatically, true or false
"speed": 1000, // Integer: Speed of the transition, in milliseconds
"timeout": 5000, // Integer: Time between slide transitions, in milliseconds
"pager": false, // Boolean: Show pager, true or false
"nav": false, // Boolean: Show navigation, true or false
"random": false, // Boolean: Randomize the order of the slides, true or false
"pause": false, // Boolean: Pause on hover, true or false
"pauseControls": true, // Boolean: Pause when hovering controls, true or false
"prevText": "Previous", // String: Text for the "previous" button
"nextText": "Next", // String: Text for the "next" button
"maxwidth": "", // Integer: Max-width of the slideshow, in pixels
"navContainer": "", // Selector: Where auto generated controls should be appended to, default is after the <ul>
"manualControls": "", // Selector: Declare custom pager navigation
"namespace": "rslides", // String: change the default namespace used
"before": $.noop, // Function: Before callback
"after": $.noop // Function: After callback
}, options);

return this.each(function () {

// Index for namespacing
i++;

var $this = $(this),

// Local variables
vendor,
selectTab,
startCycle,
restartCycle,
rotate,
$tabs,

// Helpers
index = 0,
$slide = $this.children(),
length = $slide.length,
fadeTime = parseFloat(settings.speed),
waitTime = parseFloat(settings.timeout),
maxw = parseFloat(settings.maxwidth),

// Namespacing
namespace = settings.namespace,
namespaceIdx = namespace + i,

// Classes
navClass = namespace + "_nav " + namespaceIdx + "_nav",
activeClass = namespace + "_here",
visibleClass = namespaceIdx + "_on",
slideClassPrefix = namespaceIdx + "_s",

// Pager
$pager = $("<ul class='" + namespace + "_tabs " + namespaceIdx + "_tabs' />"),

// Styles for visible and hidden slides
visible = {"float": "left", "position": "relative", "opacity": 1, "zIndex": 2},
hidden = {"float": "none", "position": "absolute", "opacity": 0, "zIndex": 1},

// Detect transition support
supportsTransitions = (function () {
var docBody = document.body || document.documentElement;
var styles = docBody.style;
var prop = "transition";
if (typeof styles[prop] === "string") {
return true;
}
// Tests for vendor specific prop
vendor = ["Moz", "Webkit", "Khtml", "O", "ms"];
prop = prop.charAt(0).toUpperCase() + prop.substr(1);
var i;
for (i = 0; i < vendor.length; i++) {
if (typeof styles[vendor[i] + prop] === "string") {
return true;
}
}
return false;
})(),

// Fading animation
slideTo = function (idx) {
settings.before(idx);
// If CSS3 transitions are supported
if (supportsTransitions) {
$slide
.removeClass(visibleClass)
.css(hidden)
.eq(idx)
.addClass(visibleClass)
.css(visible);
index = idx;
setTimeout(function () {
settings.after(idx);
}, fadeTime);
// If not, use jQuery fallback
} else {
$slide
.stop()
.fadeOut(fadeTime, function () {
$(this)
.removeClass(visibleClass)
.css(hidden)
.css("opacity", 1);
})
.eq(idx)
.fadeIn(fadeTime, function () {
$(this)
.addClass(visibleClass)
.css(visible);
settings.after(idx);
index = idx;
});
}
};

// Random order
if (settings.random) {
$slide.sort(function () {
return (Math.round(Math.random()) - 0.5);
});
$this
.empty()
.append($slide);
}

// Add ID's to each slide
$slide.each(function (i) {
this.id = slideClassPrefix + i;
});

// Add max-width and classes
$this.addClass(namespace + " " + namespaceIdx);
if (options && options.maxwidth) {
$this.css("max-width", maxw);
}

// Hide all slides, then show first one
$slide
.hide()
.css(hidden)
.eq(0)
.addClass(visibleClass)
.css(visible)
.show();

// CSS transitions
if (supportsTransitions) {
$slide
.show()
.css({
// -ms prefix isn't needed as IE10 uses prefix free version
"-webkit-transition": "opacity " + fadeTime + "ms ease-in-out",
"-moz-transition": "opacity " + fadeTime + "ms ease-in-out",
"-o-transition": "opacity " + fadeTime + "ms ease-in-out",
"transition": "opacity " + fadeTime + "ms ease-in-out"
});
}

// Only run if there's more than one slide
if ($slide.length > 1) {

// Make sure the timeout is at least 100ms longer than the fade
if (waitTime < fadeTime + 100) {
return;
}

// Pager
if (settings.pager && !settings.manualControls) {
var tabMarkup = [];
$slide.each(function (i) {
var n = i + 1;
tabMarkup +=
"<li>" +
"<a href='#' class='" + slideClassPrefix + n + "'>" + n + "</a>" +
"</li>";
});
$pager.append(tabMarkup);

// Inject pager
if (options.navContainer) {
$(settings.navContainer).append($pager);
} else {
$this.after($pager);
}
}

// Manual pager controls
if (settings.manualControls) {
$pager = $(settings.manualControls);
$pager.addClass(namespace + "_tabs " + namespaceIdx + "_tabs");
}

// Add pager slide class prefixes
if (settings.pager || settings.manualControls) {
$pager.find('li').each(function (i) {
$(this).addClass(slideClassPrefix + (i + 1));
});
}

// If we have a pager, we need to set up the selectTab function
if (settings.pager || settings.manualControls) {
$tabs = $pager.find('a');

// Select pager item
selectTab = function (idx) {
$tabs
.closest("li")
.removeClass(activeClass)
.eq(idx)
.addClass(activeClass);
};
}

// Auto cycle
if (settings.auto) {

startCycle = function () {
rotate = setInterval(function () {

// Clear the event queue
$slide.stop(true, true);

var idx = index + 1 < length ? index + 1 : 0;

// Remove active state and set new if pager is set
if (settings.pager || settings.manualControls) {
selectTab(idx);
}

slideTo(idx);
}, waitTime);
};

// Init cycle
startCycle();
}

// Restarting cycle
restartCycle = function () {
if (settings.auto) {
// Stop
clearInterval(rotate);
// Restart
startCycle();
}
};

// Pause on hover
if (settings.pause) {
$this.hover(function () {
clearInterval(rotate);
}, function () {
restartCycle();
});
}

// Pager click event handler
if (settings.pager || settings.manualControls) {
$tabs.bind("click", function (e) {
e.preventDefault();

if (!settings.pauseControls) {
restartCycle();
}

// Get index of clicked tab
var idx = $tabs.index(this);

// Break if element is already active or currently animated
if (index === idx || $("." + visibleClass).queue('fx').length) {
return;
}

// Remove active state from old tab and set new one
selectTab(idx);

// Do the animation
slideTo(idx);
})
.eq(0)
.closest("li")
.addClass(activeClass);

// Pause when hovering pager
if (settings.pauseControls) {
$tabs.hover(function () {
clearInterval(rotate);
}, function () {
restartCycle();
});
}
}

// Navigation
if (settings.nav) {
var navMarkup =
"<a href='#' class='" + navClass + " prev'>" + settings.prevText + "</a>" +
"<a href='#' class='" + navClass + " next'>" + settings.nextText + "</a>";

// Inject navigation
if (options.navContainer) {
$(settings.navContainer).append(navMarkup);
} else {
$this.after(navMarkup);
}

var $trigger = $("." + namespaceIdx + "_nav"),
$prev = $trigger.filter(".prev");

// Click event handler
$trigger.bind("click", function (e) {
e.preventDefault();

var $visibleClass = $("." + visibleClass);

// Prevent clicking if currently animated
if ($visibleClass.queue('fx').length) {
return;
}

// Adds active class during slide animation
// $(this)
// .addClass(namespace + "_active")
// .delay(fadeTime)
// .queue(function (next) {
// $(this).removeClass(namespace + "_active");
// next();
// });

// Determine where to slide
var idx = $slide.index($visibleClass),
prevIdx = idx - 1,
nextIdx = idx + 1 < length ? index + 1 : 0;

// Go to slide
slideTo($(this)[0] === $prev[0] ? prevIdx : nextIdx);
if (settings.pager || settings.manualControls) {
selectTab($(this)[0] === $prev[0] ? prevIdx : nextIdx);
}

if (!settings.pauseControls) {
restartCycle();
}
});

// Pause when hovering navigation
if (settings.pauseControls) {
$trigger.hover(function () {
clearInterval(rotate);
}, function () {
restartCycle();
});
}
}

}

// Max-width fallback
if (typeof document.body.style.maxWidth === "undefined" && options.maxwidth) {
var widthSupport = function () {
$this.css("width", "100%");
if ($this.width() > maxw) {
$this.css("width", maxw);
}
};

// Init fallback
widthSupport();
$(window).bind("resize", function () {
widthSupport();
});
}

});

};
})(jQuery, this, 0);

$(function() {
$(".rslides").responsiveSlides();
});
.rslides {
position: relative;
list-style: none;
overflow: hidden;
width: 100%;
padding: 0;
margin: 0;
}

.rslides li {
-webkit-backface-visibility: hidden;
position: absolute;
display: none;
width: 100%;
left: 0;
top: 0;
}

.rslides li:first-child {
position: relative;
display: block;
float: left;
}

.rslides img {
display: block;
height: auto;
float: left;
width: 100%;
border: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="rslides">
<li><img src="https://inmeditas.satsang-hamburg.de/headerAmrum1.jpg" alt=""></li>
<li><img src="https://inmeditas.satsang-hamburg.de/headerAmrum2.jpg" alt=""></li>
<li><img src="https://inmeditas.satsang-hamburg.de/headerAmrum3.jpg" alt=""></li>
<li><img src="https://inmeditas.satsang-hamburg.de/headerAmrum4.jpg" alt=""></li>
</ul>

关于javascript - Firefox 中的 div-background-switcher 偶尔出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57407611/

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