简体   繁体   English

有没有什么方法可以在没有 JavaScript 的情况下仅在 html 和 css 中实现此功能?

[英]Is there any way to achieve this functionality without JavaScript in html and css only?

Question Image问题图片

I want to achieve such zoom functionality in Html and CSS only because where I want to implement this doesn't supports JavaScript.我想在 Html 和 CSS 中实现这种缩放功能,只是因为我想实现它的地方不支持 JavaScript。 Is this possible?这可能吗?

Because the zoom responds to a touch or mouse event and uses its position this is not possible without JS, you can however just enlarge the image on hover, also works on mobile.由于缩放响应触摸或鼠标事件并使用其 position 没有 JS 是不可能的,但是您可以在 hover 上放大图像,也适用于移动设备。

 img.zoomer { transition: transform 0.3s; } img.zoomer:hover { transform: scale(1.5); } /* css below is irrelevant */ body { margin: 0; }.center { display: flex; justify-content: center; align-items: center; width: 100vw; height: 100vh; }
 <div class="center"> <img class="zoomer" src="https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png" width="300"> </div>

In order to be able to zoom on hover with an image inserted with <img> without javascript try this.为了能够在没有 javascript 的情况下使用插入<img>的图像放大 hover,试试这个。

 #imageZoom{
  overflow:hidden;
  transition: all 1s;
  width:200px;
  height:200px;
}

#imageZoom img:hover{
  transform: scale(1.2);
}
<div id="imageZoom">
  <img src="http://duncanlock.net/images/posts/better-figures-images-plugin-for-pelican/dummy-200x200.png">
</div>

Yes, you can get the detail image to show when you hover on the main image.是的,您可以在主图像上显示 hover 时显示的详细图像。 Here's an example:这是一个例子:

 .main, .detail { width: 200px; height: 200px; }.main { background-color: green; } img.detail { display: none; background-color: brown; position: absolute; top: 0; left: 0; } div.container:hover img.detail { display: block; }.container { width: auto; height: auto; }
 <div class="container"> <img class="main" src=""/> <img class="detail" src=""/> </div>

If you want several bits to have detail then you could have several divs which are positioned at the right places (in terms of % distance left and right of the main image) and when the user hovers over one show its related detail.如果您希望几个位具有细节,那么您可以有几个 div 位于正确的位置(根据主图像的左右距离百分比),并且当用户将鼠标悬停在一个上时会显示其相关细节。

Today all about is JS.今天的一切都是 JS。 But you can do wonderful things in CSS only and...但是你只能在 CSS 中做美妙的事情,而且......

Of course YOU CAN DO EXACTLY THAT in vanillaCSS当然你可以在 vanillaCSS 中做到这一点
Here are TWO demonstration examples.这里有两个演示示例。

  1. Louping effect on the image itself.放大图像本身的效果。 That works in fluent layouts and mostover is the chosed solution.这适用于流畅的布局,而且最重要的是选择的解决方案。

  2. The demonstration for the louping effect you asked for.您要求的放大效果演示。 That is a boxed design with a setup done in css vars.这是一个盒装设计,在 css vars 中完成设置。 So, if you want to make it repsonsive up to now you need to define the css vars to the different breakpoints.因此,如果你想让它到目前为止具有响应性,你需要将 css 变量定义为不同的断点。 (Head up: the loupe box has to be placed in the container as in the example and is actual shown below the original image. But you are free to position it anywhere else - ie by positioning it absolute/fixed/floated... - as the html structure keeps untouched.) (抬头:放大镜盒必须像示例中一样放置在容器中,并且实际显示在原始图像下方。但是您可以在其他任何地方随意使用 position - 即通过将其定位为绝对/固定/浮动... -由于 html 结构保持不变。)

Notice: more scaling up as it is needed is not a problem to both demonstrations.注意:根据需要扩大规模对两个演示来说都不是问题。

 html { font-family: sans-serif; } /* zoomBox */.zoomBox { position: relative; width: 400px; overflow: hidden; }.zoomBox * { -webkit-box-sizing: border-box; box-sizing: border-box; }.zoomBox img { position: relative; width: 100%; height: auto; z-index: -1; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover { position: absolute; width: 33.333333%; height: 33.333333%; }.zoomBox.hover.TL { top: 0; left: 0; }.zoomBox.hover.TL:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: top left; -ms-transform-origin: top left; transform-origin: top left; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.TC { top: 0; left: 33.333333%; }.zoomBox.hover.TC:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: top center; -ms-transform-origin: top center; transform-origin: top center; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.TR { top: 0; right: 0; }.zoomBox.hover.TR:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: top right; -ms-transform-origin: top right; transform-origin: top right; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.CL { top: 33.333333%; left: 0; }.zoomBox.hover.CL:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: center left; -ms-transform-origin: center left; transform-origin: center left; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.CC { top: 33.333333%; left: 33.333333%; }.zoomBox.hover.CC:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: center center; -ms-transform-origin: center center; transform-origin: center center; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.CR { top: 33.333333%; right: 0; }.zoomBox.hover.CR:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: center right; -ms-transform-origin: center right; transform-origin: center right; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.BL { bottom: 0; left: 0; }.zoomBox.hover.BL:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: bottom left; -ms-transform-origin: bottom left; transform-origin: bottom left; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.BC { bottom: 0; left: 33.333333%; }.zoomBox.hover.BC:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: bottom center; -ms-transform-origin: bottom center; transform-origin: bottom center; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.zoomBox.hover.BR { bottom: 0; right: 0; }.zoomBox.hover.BR:hover ~ img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: bottom right; -ms-transform-origin: bottom right; transform-origin: bottom right; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; } /* imageLouping */.imageLoupingBox { --imageWidth: 400px; --aspectRatio: 0.625; --imageHeight: calc( var(--imageWidth) * var(--aspectRatio) ); --hoverWidth: calc( var(--imageWidth) * 0.33333334); --hoverHeight: calc( var(--imageHeight) * 0.33333334); position: relative; }.imageLoupingBox * { -webkit-box-sizing: border-box; box-sizing: border-box; }.imageLoupingBox img.imageLoupe { position: relative; width: var(--imageWidth); height: var(--imageHeight); z-index: -1; }.imageLoupingBox.loupeBox { position: relative; width: var(--imageWidth); height: var(--imageHeight); overflow: hidden; border: 1px solid green; }.imageLoupingBox.loupeBox img { width: 100%; height: auto; -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); opacity: 0; -webkit-transition: all 0.1s; -o-transition: all 0.1s; transition: all 0.1s; }.imageLoupingBox.loupeBox:after { content: "CSS LOUPE"; display: block; width: 100%; height: 12px; text-align: center; position: absolute; top: 0; right: 0; bottom: 0; left: 0; margin: auto; z-index: -2; font-family: sans-serif; opacity: 1; -webkit-transition: all 0.05s; -o-transition: all 0.05s; transition: all 0.05s; }.imageLoupingBox.hover { position: absolute; width: var(--hoverWidth); height: var(--hoverHeight); }.imageLoupingBox.hover:hover ~.loupeBox:after { opacity: 0; -webkit-transition: all 0.05s; -o-transition: all 0.05s; transition: all 0.05s; }.imageLoupingBox.hover:hover ~.loupeBox img { opacity: 1; -webkit-transition: all 0.1s; -o-transition: all 0.1s; transition: all 0.1s; }.imageLoupingBox.hover.TL { top: 0; left: 0; }.imageLoupingBox.hover.TL:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: top left; -ms-transform-origin: top left; transform-origin: top left; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.TC { top: 0; left: var(--hoverWidth); }.imageLoupingBox.hover.TC:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: top center; -ms-transform-origin: top center; transform-origin: top center; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.TR { top: 0; left: calc( var(--hoverWidth) * 2 ); }.imageLoupingBox.hover.TR:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: top right; -ms-transform-origin: top right; transform-origin: top right; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.CL { top: var(--hoverHeight); left: 0; }.imageLoupingBox.hover.CL:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: center left; -ms-transform-origin: center left; transform-origin: center left; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.CC { top: var(--hoverHeight); left: var(--hoverWidth); }.imageLoupingBox.hover.CC:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: center center; -ms-transform-origin: center center; transform-origin: center center; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.CR { top: var(--hoverHeight); left: calc( var(--hoverWidth) * 2 ); }.imageLoupingBox.hover.CR:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: center right; -ms-transform-origin: center right; transform-origin: center right; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.BL { top: calc( var(--hoverHeight) * 2); left: 0; }.imageLoupingBox.hover.BL:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: bottom left; -ms-transform-origin: bottom left; transform-origin: bottom left; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.BC { top: calc( var(--hoverHeight) * 2); left: var(--hoverWidth); }.imageLoupingBox.hover.BC:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: bottom center; -ms-transform-origin: bottom center; transform-origin: bottom center; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }.imageLoupingBox.hover.BR { top: calc( var(--hoverHeight) * 2); left: calc( var(--hoverWidth) * 2 ); }.imageLoupingBox.hover.BR:hover ~.loupeBox img { -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transform-origin: bottom right; -ms-transform-origin: bottom right; transform-origin: bottom right; -webkit-transition: all 1s; -o-transition: all 1s; transition: all 1s; }
 <h1>ZOOMING EFFECTS vanillaCSS</h1> <h2>Zoom box: enlarge image itself</h2> <div class="zoomBox"> <div class="hover TL"></div> <div class="hover TC"></div> <div class="hover TR"></div> <div class="hover CL"></div> <div class="hover CC"></div> <div class="hover CR"></div> <div class="hover BL"></div> <div class="hover BC"></div> <div class="hover BR"></div> <img src="https://source.unsplash.com/qpemSW6_1Z0"> </div> <hr> <h2>Image with a cssLOUPE</h2> <div class="imageLoupingBox"> <div class="hover TL"></div> <div class="hover TC"></div> <div class="hover TR"></div> <div class="hover CL"></div> <div class="hover CC"></div> <div class="hover CR"></div> <div class="hover BL"></div> <div class="hover BC"></div> <div class="hover BR"></div> <img class="imageLoupe" src="https://source.unsplash.com/qpemSW6_1Z0"> <div class="loupeBox"> <.--<img src="images/demoImage:jpg">--> <img src="https.//source.unsplash.com/qpemSW6_1Z0"> </div> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有更好的方法来实现没有Javascript的CSS动画? - Is there a better way to achieve this CSS animation without Javascript? 我需要使用 HTML、CSS 和 JavaScript 来实现这一点 - 任何帮助都会很棒 - I need to achieve this using HTML, CSS and JavaScript - any help will be great 如何使用css和javascript实现打印预览功能。 - How to Achieve print preview functionality with css and javascript .? 无需使用jQuery或任何其他库即可实现blockUI功能的最佳方法? - Best way to achieve blockUI functionality WITHOUT using jQuery or any other library? 没有jQuery,仅使用CSS3和HTML5,有什么方法可以做同样的事情 - Is there any way to make same thing without jQuery, and using only CSS3 and HTML5 隐藏没有javascript的HTML元素,只有CSS - Hide HTML elements without javascript, only CSS 如何在没有任何 html 的情况下使用香草 javascript 和 css 制作切换按钮 - How to make a toggle button with vanilla javascript and css without any html 有什么方法可以将使用Javascript部分样式化的网页转换为纯HTML和CSS? - Is there any way to turn a webpage that is styled partially with Javascript into pure HTML and CSS? 有没有办法在 CSS 中使用 Javascript 变量来自定义颜色,或者实现此目的的替代方法? - Is there a way to use a Javascript variable in CSS to customize colour, or an alternative way to achieve this? 有没有办法只在内部资源管理器上使用 Javascript、HTML、CSS(无 Jquery) - is there a way to do onChange() on internent explorer only with Javascript, HTML, CSS (No Jquery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM