简体   繁体   English

jQuery更改div位置onMouseOver

[英]jQuery change div position onMouseOver

I have the svg map with pins on it. 我有带针的svg地图。 I want to make the application that shows the description of the pin, when user put the mouse on it. 当用户将鼠标放在该引脚上时,我想制作一个显示该引脚描述的应用程序。 The thing is, that the description should be in the mouse position. 事实是,描述应该在鼠标位置。 I've already done things like changing color onMouseOver and manipulate with all different css parameters. 我已经做过诸如更改onMouseOver的颜色并使用所有不同的CSS参数进行操作的操作。 But I have problem with changing the div position. 但是我在更改div位置时遇到问题。

In the same part of code when I put: 在我输入的同一部分代码中:

document.getElementById("test").style.color = "red";

the color is changing. 颜色正在改变。

but when I do this: 但是当我这样做时:

document.getElementById("test").style.left = 500;

nothing happens. 什么都没发生。

I was trying with all those solution: 我正在尝试所有这些解决方案:

$("test").css({top: 200, left: 200});

and lots of others, but I have no idea why it doesnt work. 和很多其他人,但我不知道为什么它不起作用。

my jQuery code: 我的jQuery代码:

jQuery(document).ready(function() {
    jQuery('img.svg').each(function(){
        var mouseX;
        var mouseY;
        var $img = jQuery(this);
        var imgURL = $img.attr('src');

        jQuery.get(imgURL, function (data) {
        // Get the SVG tag, ignore the rest
        var $svg = jQuery(data).find('svg');

        // Replace image with new SVG
        $img.replaceWith($svg);

        // Add an handler
        jQuery('#pins path').each(function () {


            jQuery(this).click(function () {
                var post_slug = jQuery(this).attr('id');
                var url = "http://127.0.0.1:8000/posts/post_slug".replace("post_slug", post_slug); //TODO
                window.location = url;
            });
            jQuery(this).mouseover(function (e) {
                mouseX = e.pageX;
                mouseY = e.pageY;
                var post_slug = jQuery(this).attr('id');
                // using string concat due to name conficts with "path id" in svg map
                //document.getElementById("popup_".concat(post_slug)).style.display = 'block';
                document.getElementById("popup_".concat(post_slug)).style.top = mouseY;
                document.getElementById("popup_".concat(post_slug)).style.left = mouseX;
                document.getElementById("popup_".concat(post_slug)).style.color = "red";
            });
            jQuery(this).mouseout(function () {
                var post_slug = jQuery(this).attr('id');
                // using string concat due to name conficts with "path id" in svg map
                document.getElementById("popup_".concat(post_slug)).style.display = 'none';
            });
        });
    });

});

}); });

The divs are dynamically created according to objects in my django template. div是根据django模板中的对象动态创建的。

for (var i = 0; i < pin_slugs.length; i++) {
    var popup = document.createElement("div");
    popup.id = "popup_".concat(pin_slugs[i]);
    popup.title = pin_slugs[i];
    popup.innerHTML = pin_slugs[i];
    document.body.appendChild(popup);
}

I'm struggling with it for a long time. 我已经为此苦苦挣扎了很长时间。 Can anyone help? 有人可以帮忙吗?

left, right, top, bottom properties will not work with default position value which is static. 左,右,上,下属性不适用于静态的默认位置值。 You should give position:absolute/relative. 您应该给出位置:绝对/相对。 Best one to give in this scenario is absolute. 在这种情况下,最好的选择是绝对的。

I solved it out. 我解决了。 The solution was simple. 解决方案很简单。

mouseX + "px";

and in my css I needed to put: 在我的CSS中,我需要输入:

position: absolute;

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

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