我一直在寻找我想要在google中找到的示例,然后在CodePen中找到了一个示例,然后尝试将html,css和javascript代码传递到我的项目中,然后在我的项目中不起作用。我也在BlazorFiddle中尝试过,结果相同,没有任何效果。我真的不知道是什么原因造成的。
(我将通过CodePen中的代码进行传递,以实现更好的可视化效果)
HTML代码:
<script src="https://unpkg.com/animate-css-grid@1.4.0/dist/main.js" > </script>
<div class="p-4">
<div class="mb-4">
<p>this pen uses <a target="__blank" href="https://github.com/aholachek/animate-css-grid">animate-css-grid</a>
</p>
<p>
If you use React, you might want to try out <a href="https://github.com/aholachek/react-flip-toolkit" target="__blank">react-flip-toolkit</a>
</p>
<button class="btn js-toggle-grid-gap">toggle <code>grid-gap</code></button>
<button class="btn js-toggle-grid-columns">toggle <code>grid-template-columns</code></button>
<p>click a card to toggle the <code>grid-column</code> and <code>grid-row </code>properties on the card</p>
</div>
<div class="grid grid--full mb-4">
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div><div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
<div class="card">
<div>
<img src="https://1h4hfe10xz8m3g3xkh2wb9lc-wpengine.netdna-ssl.com/blog/files/2015/08/thestocks-imagem.jpg" class="card__img"/>
</div>
</div>
</div>
<button class="btn js-add-card">add a card</button>
</div>
CSS代码:
/* layout */
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr));
grid-auto-rows: 12rem;
grid-gap: 16px;
grid-auto-flow: dense;
}
.grid--big-columns {
grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr));
}
.grid--big-gap {
grid-gap: 2.5rem;
}
/* styling */
.card--expanded {
grid-column: span 3;
grid-row: span 3;
}
.card--expanded > .card__img {
transform: scale(1.03);
}
@keyframes fadeIn {
0% {
opacity: 0;
transform: scale(0);
}
100% {
opacity: 1;
transform: scale(1);
}
}
.fade-in {
opacity: 0;
animation: fadeIn 0.4s forwards;
animation-delay: 0.3s;
}
.card {
cursor: pointer;
overflow: hidden;
position: relative;
}
.card__img {
transition: transform 1s;
}
body {
background-color: #191919;
color: lightgray;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI",
"Roboto", "Helvetica Neue", Arial, sans-serif;
font-size: 1.1rem;
}
.mb-4 {
margin-bottom: 1rem;
}
.p-4 {
padding: 1rem;
}
button {
padding: 0.75rem;
margin-right: 0.75rem;
background-color: #191919;
color: lightgray;
border: 1px solid lightgray;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: hsla(0, 0%, 83%, 0.05);
}
button:focus {
box-shadow: 0 0 0 3px #7396e4;
outline: none;
}
a {
font-weight: bold;
color: #7396e4;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
code {
color: #7396e4;
}
JS代码:
window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;
const grid = document.querySelector(".grid");
// event handler to toggle grid sizing
document
.querySelector(".js-toggle-grid-columns")
.addEventListener("click", () => grid.classList.toggle("grid--big-columns"));
document
.querySelector(".js-toggle-grid-gap")
.addEventListener("click", () => grid.classList.toggle("grid--big-gap"));
const addCard = () => {
return fetch(
`https://source.unsplash.com/random/${Math.floor(Math.random() * 1000)}`
)
};
// event handler to add a new card
document.querySelector(".js-add-card").addEventListener("click", addCard);
// event handler to toggle card size on click
grid.addEventListener("click", ev => {
let target = ev.target;
while (target.tagName !== "HTML") {
if (target.classList.contains("card")) {
target.classList.toggle("card--expanded");
return;
}
target = target.parentElement;
}
});
Promise.all([...Array(10).keys()].map(addCard)).then(() => {
animateCSSGrid.wrapGrid(grid, {
duration: 350,
stagger: 10,
onStart: elements =>
console.log(`started animation for ${elements.length} elements`),
onEnd: elements =>
console.log(`finished animation for ${elements.length} elements`)
});
});
这里有两个链接:CodePen和BlazorFiddle。
感谢您的关注。
答案 0 :(得分:1)
在Blazor渲染您要引用的元素之前,将调用您的js代码。
要使其正常工作,请执行以下操作:
将整个js包装在一个函数中。我们称它为init。在使用它时,请删除window.CP.PenTimer.MAX_TIME_IN_LOOP_WO_EXIT = 6000;
行,我猜这是CodePen特有的
<script>
function init() {
const grid = document.querySelector(".grid");
// (...)
}
</script>
在您的组件中,在@page
指令后添加以下内容
@inject IJSRuntime jsRuntime
这将注入您从blazor进行js互操作所需的内容。
然后将以下代码添加到组件代码的末尾以调用该方法:
@code {
protected override async Task OnAfterRenderAsync(bool isFirst) {
if (isFirst) {
// invokes the init function once Blazor is initiated and
// your component has rendered the first time.
await jsRuntime.InvokeAsync<object>("init");
}
}
}