gpt4 book ai didi

javascript - d3.js 更新模式未按预期工作

转载 作者:行者123 更新时间:2023-11-29 10:33:20 25 4
gpt4 key购买 nike

我在 D3 v4 中写了一个小灯箱,点击时会加载三张图片:选定的一张、组中的上一张和下一张。

我将对这些图像的引用存储在与 general update pattern 一起使用的数组中.然后,在单击“上一个”或“下一个”按钮后,数组被修改,以便在两端添加一个图像并删除一个图像:

[image1, image2, image3] > '下一步' > [image2, image3, image4]

[image1, image2, image3] > 'prev' > [image99, image1, image2]

换句话说,一张图片应该“退出”,两张应该“更新”,一张应该“进入”。从视觉上看,它应该是这样的:

Update pattern after clicking 'next'

不幸的是,它没有按设计工作,我也不确定为什么。在控制台中,我看到两个“退出”、一个“更新”和两个“进入”。为什么?

这是我的 pen相关的代码块如下。感谢对此的任何帮助。

d3.selectAll( 'div[role="img"]' ) // image group for lightbox
.on( "mousedown touchstart", function( d, i, n ) {
luxBox( d, i, n );
});

function luxBox( d, idx, n ) {

var l = n.length,
j = idx, //selected image in group
jp = j > 0 ? j - 1 : l - 1, //preceding image in group
jn = j < l - 1 ? j + 1 : 0; //following image in group

var imgs = [
d3.select(n[jp]), //0
d3.select(n[j]), //1
d3.select(n[jn]) //2
];

function update(data) {

var t = d3.transition().duration(400);

var lux = d3.select('#luxbox')
.attr("class", "show")
.select('#slider')
.selectAll('li')
.data(data, function(d) { return d; }); //join

lux //exit
.exit()
.attr("class", "exit")
.transition(t)
.style("opacity", function(d, i) {
console.log('exit: i=' + i);
return 0;
})
.remove();

lux //update
.attr("class", "update")
.transition(t)
.style("transform", function(d, i) {
console.log( 'update: i=' + i );
return "translateX(" + (i * 100 - 100) + "%)";
});

lux //enter
.enter()
.append('li')
.attr("class", "enter")
.style("opacity", 0)
.style("background-image", function(d) { return d.style('background-image'); })
.style("transform", function(d, i) {
console.log( 'enter: i=' + i );
return "translateX(" + (i * 100 - 100) + "%)";
})
//append("h3") // caption
.transition(t)
.style("opacity", 1);
}

update(imgs);

d3.select(".next")
.on("mousedown touchstart", function() {
idx < l - 1 ? idx++ : idx = 0; //next index
console.log( 'index=' + idx );
jn = idx < l - 1 ? idx + 1 : 0; //new following image
imgs.push( d3.select(n[jn]) );
imgs.shift();
update( imgs );
});
d3.select(".prev")
.on("mousedown touchstart", function() {
idx > 0 ? idx-- : idx = l - 1; //prev index
console.log( 'index=' + idx );
jp = idx > 0 ? idx - 1 : l - 1; //new preceding image
imgs.unshift( d3.select(n[jp]) );
imgs.pop();
update( imgs );
});
d3.select(".close")
.on("mousedown touchstart", function() {
d3.select('#luxbox')
.attr("class", "hide");
setTimeout( function() {
d3.selectAll('#slider li').remove();
console.log('slides removed')
}, 400 );
});
}

最佳答案

进一步调查并发现一些great answers , 我有 solved我的问题是使用数据 key 匹配图像选择索引。

var imgs = [
{
"url": d3.select(n[jp]).select("img").attr("src"),
"caption": d3.select(n[jp]).select("img").attr("alt"),
"key": jp
},
{
"url": d3.select(n[j]).select("img").attr("src"),
"caption": d3.select(n[j]).select("img").attr("alt"),
"key": j
},
{
"url": d3.select(n[jn]).select("img").attr("src"),
"caption": d3.select(n[jn]).select("img").attr("alt"),
"key": jn
}
];

// ...

.data(data, function(d) { return d.key; }); //join

关于javascript - d3.js 更新模式未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41171979/

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