简体   繁体   English

如何单击打开图像的链接,然后仅显示该图像的一部分

[英]How can I click on a link which opens an image, then only display part of that image

I have a large image of a map with points of interest on it. 我有一张大地图,上面有兴趣点。

What I want is to have a button on a page of text, when the button is clicked it opens the map image in a different window. 我想要的是在文本页面上有一个按钮,单击该按钮会在另一个窗口中打开地图图像。 What I then need is for the image to only display the relevant portion of the map showing the point of interest mentioned on the original page with the button. 然后,我需要的是图像仅显示地图的相关部分,该部分显示了带有按钮的原始页面上提到的兴趣点。

I've found ways to show a certain section of the map using and coordinates, or using the map as a sprite sheet, or using CSS background-postion, but I can't find a way to implement this on clicking the button. 我已经找到了使用和坐标,或者将地图用作Sprite表格,或者使用CSS背景位置来显示地图的特定部分的方法,但是我找不到在单击按钮时实现此方法的方法。

Ideally I'd like to achieve this with just CSS because there are going to be quite a few pages linking to this image. 理想情况下,我只想用CSS来实现这一点,因为将有很多页面链接到该图像。

Here is a small guide of what I'm tring to achieve. 这是我要实现的目标的小指南。

Image showing how this works 该图显示了它如何工作

    <style>

.map-one {
  background: url('map.jpg');
  background-position: center bottom;
  height: 300px;
  width: 300px;
}

</style>

<button><a class="map-one" href="map.jpg">Click</a></button>

This is an exmaple of some code I've tried, which is obviously wrong, but I don't know how to apply the css style to the image when clicking on the link. 这是我尝试过的一些代码的范例,这显然是错误的,但是当单击链接时,我不知道如何将css样式应用于图像。

Well it doesn't work with just pure css, you have to pass some parameters to your new window. 好吧,它不仅仅适用于纯CSS,您必须将一些参数传递到新窗口。 So i will assume that you pass an X and Y coordinate to the new window and you have that available on your new page. 因此,我假设您将X和Y坐标传递到新窗口,并且在新页面上可以使用该坐标。

First you need to wrap the "Map" to give it a viewport. 首先,您需要包装“地图”以为其提供视口。 If nothing else is on the page you can theoretically use body: 如果页面上没有其他内容,理论上您可以使用body:

<div id="mapviewport">
<div id="map">
</div>
</div>

So if you want the user to be able to explore the map you can use overflow: auto on the viewport, otherwise use overflow: hidden . 因此,如果希望用户能够浏览地图,则可以在视口上使用overflow: auto ,否则可以使用overflow: hidden The map container gets the width/height of the map. 地图容器获取地图的宽度/高度。 The map is provided via background-image on the map-container. 该地图是通过地图容器上的background-image提供的。

Now to scroll to the right position, use . 现在滚动到正确的位置,使用。 scrollTop and .scrollLeft on the mapViewport to scroll the map to the right spot. .scrollLeft上的scrollTop.scrollLeft将地图滚动到正确的位置。

You will need to calibrate the values until you have achieved the exact area you want, but the code will be pretty much like this: 您将需要校准值,直到获得所需的确切区域为止,但是代码非常像这样:

Main page 主页

<a href="map.html" target="_blank"><button> <!-- Map page path -->
    CLICK ME!
</button></a>

Map page 地图页面

<style>
    div.map {
        background-image: url('map.png'); /* image file path */
        background-position: 70px 90px;   /* image position  */
        width: 200px;                     /* image size      */
        height: 200px;
    }
</style>
<div class="map"></div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM