gpt4 book ai didi

javascript - 将鼠标悬停在缩略图上以将主图像更改为两倍大小的缩略图,并且主图像应链接到与缩略图相同的网站

转载 作者:行者123 更新时间:2023-11-27 23:34:57 25 4
gpt4 key购买 nike

我研究了几个与我的问题类似的脚本,并尝试修改它们以使其适合我,但无济于事。我想要做的是 mouseover 任何缩略图并将主图像更改为该特定缩略图并将其渲染为两倍大小。我尝试的最后一件事是将主图像链接到与该缩略图链接相同的网站。我想在 jquery 中使用一些 css 来做到这一点。我在 jsfiddle 中看到了类似的东西。我认为我卡住的地方是无法完全理解如何更改 src 属性。如果有任何帮助,我将不胜感激。

   <div id="header">
<a href="http://www.pendilum.com" target="_blank">
<img id="main" src="images/people1.jpg" width="400" height="400" alt="people">
</a>
</div>
<div id="footer">
<a href="http://www.pendilum.com" target="_blank">
<img class="thumbs" src="images/people1.png" width="auto" height="auto" alt="people">
</a>
<a href="http://www.power.com" target="_blank">
<img class="thumbs" src="images/people2.png" width="auto" height="auto" alt="handshake">
</a>
<a href="http://www.pants.com" target="_blank">
<img class="thumbs" src="images/people3.png" width="auto" height="auto" alt="peoplejoined">
</a>
<a href="http://www.possible.com" target="_blank">
<img class="thumbs" src="images/people4.png" width="auto" height="auto" alt="unisex">
</a>
<a href="http://www.yoga.com" target="_blank">
<img class="thumbs" src="images/people5.png" width="auto" height="auto" alt="yoga">
</a>
</div>

这是JS代码

  window.onload = function(){
$(document).ready(function(){
var mainImg = $("#main");
$(".thumbs").mouseover(function(){
var src = $(this).attr("src");
$("#main").attr("src",src);
$(this).css("transform","scale(2)")

});
});
;}

最佳答案

你很接近。

https://jsfiddle.net/j536kzxx/3/

如果您使用的是 document.ready,则不需要 window.onload。

$(document).ready(function(){
var $mainImg = $("#main");
var $mainLink = $("#header > a");
$(".thumbs").mouseover(function(){
var src = $(this).attr("src");
var link = $(this).parent().attr("href");
var doubleWidth = $(this).width()*2;
var doubleHeight = $(this).height()*2;
$mainImg.attr("src",src);
$mainImg.css({"width":doubleWidth,"height:":doubleHeight})
$mainLink.attr("href",link);
});
});

获取悬停图像的宽度/高度并将其应用于主图像。获取父 anchor 链接并将其应用于主 anchor 链接。瞧。

编辑:javascript 是问题所在。你的 html 很好,除了一件事。您应该将 400px 设置为主 img container 的宽度和高度,而不是 img 本身。这是因为您希望缩略图是其原始大小的两倍,而不是全部 400 像素。

至于parent(),它不仅仅是 anchor 链接。 所有 html 元素,<html></html> 除外可以有 parent 。父元素只是一个包含另一个元素的元素。

<html>
<head> <!-- head is child of html, sibling of body -->
</head>
<body> <!-- body is child of html, parent of p -->
<p> <!-- p is child of body, parent of span -->
<span>Hello World</span> <!-- span is child of p, it is not a parent because it doesn't contain any elements -->
</p>
</body>
<html>

它是一个层次结构,或一棵树。这张图应该有用

enter image description here

关于javascript - 将鼠标悬停在缩略图上以将主图像更改为两倍大小的缩略图,并且主图像应链接到与缩略图相同的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280377/

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