Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

html - Transition duration does not work on page refresh

I am having a slight problem with my d3 transitions.

After a page refresh, the transition occurs instantaneously. I want the first transition after the page refresh to take 1 second like the others.

JSFiddle

Here's a snippet

const name_ = d3.select('#name_container');

const M = name_
.append('text')
    .attr('y', 50)
    .attr('x', 50)
    .attr('font-size', '2em')
    .text('M')
    .attr('fill', 'blue')
    .style('pointer-events', 'none');

const M_backboard = name_
.append('rect')
    .attr('x', 50)
    .attr('y', 50)
    .attr('width', 50)
    .attr('height', 50)
    .on('mouseover', function() {
        M.transition().duration(1000)
            .attr('opacity', 0);
    })
    .on('mouseout', function() {
        M.transition().duration(1000)
            .attr('opacity', 1);
    });
<!DOCTYPE html>

<html>
    <head>
        <link rel="shortcut icon" href="#">
        <script src="https://d3js.org/d3.v6.js"></script>
    </head>
    <body>
        <svg id=name_container></svg>    
        <script src=index.js></script>
    </body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem is that the opacity on the problem element is not initially set when the page loads so there's nothing to transition from to the to. If you set the opacity to 1 by default, you will get the expected results.

EDIT Link to a forked fiddle. Is this what you're going for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...