gpt4 book ai didi

javascript - 使用 for 循环将 3 个函数合并为一个函数

转载 作者:行者123 更新时间:2023-11-28 03:36:22 26 4
gpt4 key购买 nike

我想使用 for 循环将这 3 个函数合并为一个函数。

如何设置?

完整代码:

https://jsfiddle.net/3Lky7vu4/1/

我最初尝试过这个,但由于某种原因没有成功。

  const cover = document.querySelectorAll(".jacket-left, .jacket-middle, .jacket-right");
cover.addEventListener("click", coverClickHandler);

由于上面的另一种方法在代码中本身不起作用,因此如何使用 for 循环编写此代码?

const load = (function makeLoad() {
"use strict";

function _load(tag) {
return function(url) {
return new Promise(function(resolve) {
const element = document.createElement(tag);
const parent = "body";
const attr = "src";
element.onload = function() {
resolve(url);
};
element[attr] = url;
document[parent].appendChild(element);
});
};
}
return {
js: _load("script")
};
}());

(function manageCover() {
"use strict";

function hide(el) {
el.classList.add("hide");
}

function coverClickHandler(evt) {
const cover = evt.currentTarget;
hide(cover);
}
const cover = document.querySelector(".jacket-left");
cover.addEventListener("click", coverClickHandler);
}());


(function manageCover() {
"use strict";

function hide(el) {
el.classList.add("hide");
}

function coverClickHandler(evt) {
const cover = evt.currentTarget;
hide(cover);
}
const cover = document.querySelector(".jacket-middle");
cover.addEventListener("click", coverClickHandler);
}());

(function manageCover() {
"use strict";

function hide(el) {
el.classList.add("hide");
}

function coverClickHandler(evt) {
const cover = evt.currentTarget;
hide(cover);
}
const cover = document.querySelector(".jacket-right");
cover.addEventListener("click", coverClickHandler);
}());

const videoPlayer = (function makeVideoPlayer() {
"use strict";
const players = [];

function onPlayerReady(event) {
const player = event.target;
player.setVolume(100); // percent
}

let hasShuffled = false;

function onPlayerStateChange(event) {
const player = event.target;
if (!hasShuffled) {
player.setShuffle(true);
player.playVideoAt(0);
hasShuffled = true;
}
if (event.data === YT.PlayerState.PLAYING) {
for (let i = 0; i < players.length; i++) {
if (players[i] !== event.target) players[i].pauseVideo();
}
}

const playerVars = player.b.b.playerVars;
if (playerVars.loop && event.data === YT.PlayerState.ENDED) {
player.seekTo(playerVars.start);
}
}

function addVideo(video, settings) {
players.push(new YT.Player(video, Object.assign({
videoId: video.dataset.id,
host: "https://www.youtube-nocookie.com",
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
}, settings)));
}

function init(video, settings) {
load.js("https://www.youtube.com/player_api").then(function() {
YT.ready(function() {
addVideo(video, settings);
});
});
}
return {
init
};
}());

function loadPlayer(opts) {
"use strict";

function show(el) {
el.classList.remove("hide");
}

function initPlayer(wrapper) {
const video = wrapper.querySelector(".video");
opts.width = opts.width || 198;
opts.height = opts.height || 198;
opts.autoplay = 1;
opts.controls = 1;
opts.rel = 0;
opts.iv_load_policy = 3;
opts.fs = 0;
opts.disablekb = 1;

function paramInOpts(settings, param) {
if (opts[param] !== undefined) {
settings[param] = opts[param];
}
return settings;
}
const settingsParams = ["width", "height", "videoid", "host"];
const settings = settingsParams.reduce(paramInOpts, {});
const playerVarsParams = ["autoplay", "cc_load_policy",
"controls", "disablekb", "end", "fs", "iv_load_policy",
"list", "listType", "loop", "playlist", "rel", "start"
];
settings.playerVars = playerVarsParams.reduce(paramInOpts, {});
videoPlayer.init(video, settings);
}

function coverClickHandler(evt) {
const wrapper = evt.currentTarget.nextElementSibling;
show(wrapper);
initPlayer(wrapper);
}
const cover = document.querySelector(opts.target);
cover.addEventListener("click", coverClickHandler);
}
const playlist = "0dgNc5S8cLI,mnfmQe8Mv1g,-Xgi_way56U,CHahce95B1g";

loadPlayer({
target: ".jacket-left",
width: 277,
height: 207
});

loadPlayer({
target: ".jacket-middle",
width: 277,
height: 207
});
loadPlayer({
target: ".jacket-right",
width: 277,
height: 207
});


loadPlayer({
target: ".jacketc",
width: 600,
height: 338,
loop: true,
playlist
});
loadPlayer({
target: ".alpha",
start: 0,
end: 280,
loop: true
});
loadPlayer({
target: ".beta",
start: 0,
end: 240,
loop: true
});
loadPlayer({
target: ".gamma",
start: 0,
end: 265,
loop: true
});
loadPlayer({
target: ".delta",
start: 4,
end: 254,
loop: true
});
loadPlayer({
target: ".epsilon",
start: 0,
end: 242,
loop: true
});
loadPlayer({
target: ".zeta",
start: 0,
end: 285,
loop: true
});
loadPlayer({
target: ".eta",
start: 23,
end: 312,
loop: true
});
loadPlayer({
target: ".theta",
start: 2
});
loadPlayer({
target: ".iota"
});
html,
body {
height: 100%;
padding: 0;
margin: 0;
}

body {
background: #353198;
}

.outer {
display: table;
height: 100%;
margin: 0 auto;
}

.tcell {
display: table-cell;
vertical-align: middle;
}
.container {
width: 936px;
padding: 25px;
margin: 100px auto;
border-radius: 25px;
border: 2px solid #0059dd;
background: #000000;
}
.container-top {
position: relative;
height: 310px;
margin: 0;
border-radius: 25px;
border: 3px solid #0059dd;
box-sizing: border-box;
background: url("https://i.imgur.com/jEMIULl.jpg") no-repeat 0 0;
background-size: cover;
}

.v-contain{
position: relative;
top: 50%;
transform: translateY(-50%);
float: left;
margin-left: 25px;
width: 277px;
background: red;
border-radius: 25px;
}

.jacket{
position: relative;
width: 80px;
height: 80px;
margin: auto;
background: url("https://i.imgur.com/fzdYu8g.jpg") no-repeat 0 0;
background-size: 100% 200%;
}

.jacket-middle {
background-position: 0 -80px;
background-size: 100% 200%;
}

.jacket-right {
background-position: 0 -80px;
background-size: 100% 200%;
}

.circle{
position: absolute;
top: 25%;
left: 25%;
width: 50%;
height: 50%;
box-sizing: border-box;
border: 1px solid #0059dd;
border-radius: 50%;
cursor: pointer;
background: rgba(0, 0, 0, 0.5);
transition: transform 0.1s ease 0s;
box-shadow: rgba(0, 0, 0, 0.4) 0 0 10px;
}

.play{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
width: 38px;
height: 22px;
fill: #0059dd;
}

.circle:hover {
border: 1px solid red;
transform: scale(1.1);
}

.circle:hover .play {
fill: red;
}

.wrap{
position: relative;
width: 277px;
height: 207px;
cursor: pointer;
overflow: hidden;
border-radius: 25px;
}

.wrap iframe {
position: absolute;
top: 0;
left: 0;
width: 277px;
height: 207px;
cursor: pointer;
}
.hide {
display: none;
}
<div class="outer">
<div class="tcell">
<div class="container ">
<div class="container-top">
<div class="v-contain ">
<div class="jacket jacket-left">
<div class="circle"> <svg class="play" width="38" height="22" viewBox="0 0 26 26">
<title>Play</title>
<path d="M7.712 22.04a.732.732 0 0 1-.806.007.767.767 0 0 1-.406-.703V4.656c0-.31.135-.544.406-.703.271-.16.54-.157.806.006l14.458 8.332c.266.163.4.4.4.709 0 .31-.134.546-.4.71L7.712 22.04z" fill-rule="evenodd" />
</svg> </div>
</div>
<div class="wrap wrap-left hide">
<div class="video" data-id="jMAShwisnQI"></div>
</div>
</div>
<div class="v-contain container-middle-video">
<div class="jacket jacket-middle">
<div class="circle"> <svg class="play" width="38" height="22" viewBox="0 0 26 26">
<title>Play</title>
<path d="M7.712 22.04a.732.732 0 0 1-.806.007.767.767 0 0 1-.406-.703V4.656c0-.31.135-.544.406-.703.271-.16.54-.157.806.006l14.458 8.332c.266.163.4.4.4.709 0 .31-.134.546-.4.71L7.712 22.04z" fill-rule="evenodd" />
</svg> </div>
</div>
<div class="wrap wrap-middle hide">
<div class="video" data-id="-SiGnAi845o"></div>
</div>
</div>
<div class="v-contain container-right-video">
<div class="jacket jacket-right">
<div class="circle"> <svg class="play" width="38" height="22" viewBox="0 0 26 26">
<title>Play</title>
<path d="M7.712 22.04a.732.732 0 0 1-.806.007.767.767 0 0 1-.406-.703V4.656c0-.31.135-.544.406-.703.271-.16.54-.157.806.006l14.458 8.332c.266.163.4.4.4.709 0 .31-.134.546-.4.71L7.712 22.04z" fill-rule="evenodd" />
</svg> </div>
</div>
<div class="wrap wrap-right hide">
<div class="video" data-id="-SiGnAi845o"></div>
</div>
</div>
</div>
</div>
</div>
</div>

最佳答案

您可以将 Array.from(iterable, mappingFunction) 应用于由 document.querySelectorAll() 返回的类似数组的 NodeList映射每个元素并添加点击监听器

由于 querySelectorAll() 不会直接返回数组,因此您需要使用 Array.from() 将其转换为数组,并同时应用 click 在第二个参数提供的 mappingFunction 中监听它

在下面的解决方案中,我展示了一个简单的用例,您可以应用此技术来添加点击监听器:

(function manageCover() {
"use strict";

function coverClickHandler(e){
console.log(e.target.textContent);
}

const nodeList = document.querySelectorAll(".jacket-left, .jacket-middle, .jacket-right");
const cover = Array.from(nodeList, (ele) => ele.addEventListener("click", coverClickHandler));
})();
<div class="jacket-left">jacket-left</div>
<div class="jacket-middle">jacket-middle</div>
<div class="jacket-right">jacket-right</div>

关于javascript - 使用 for 循环将 3 个函数合并为一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57697745/

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