У меня есть этот сценарий, и он делает именно то, что я хочу. показывает и скрывает div и следует за курсором мыши, и делает это только тогда, когда область просмотра имеет определенный размер и выше, но я хотел бы знать, как его сжать, чтобы весь скрипт не дублировался дважды, один для загрузки и один для при прослушивании изменения размера окна браузера.
<script>
$( document ).ready(function() { //document ready
$( window ).on( "load", function() { //entire page has loaded
if ($(window).width() < 963) {
tip = $(this).nextAll('#tail');
tip.hide();
}
else {
//Tooltips
$("#mainbody").hover(function(){
tip = $(this).nextAll('#tail');
tip.show(); //Show tooltip
}, function() {
tip.hide(); //Hide tooltip
}).mousemove(function(e) {
if ($(window).width() > 962) {
//Change these numbers to move the tooltip offset
var mousex = e.pageX + 20; //Get X coodrinates
var mousey = e.pageY + 20; //Get Y coordinates
var tipWidth = tip.width(); //Find width of tooltip
var tipHeight = tip.height(); //Find height of tooltip
//Distance of element from the right edge of viewport
var tipVisX = $(window).width() - (mousex + tipWidth);
//Distance of element from the bottom of viewport
//var tipVisY = $(window).height() - (mousey + tipHeight);
var tipVisY = $("#mainbody").height() - (tipHeight);
if (tipVisX < 1) { //If tooltip exceeds the X coordinate of viewport
//mousex = e.pageX - tipWidth - 20;
} if (tipVisY < 1) { //If tooltip exceeds the Y coordinate of viewport
//mousey = e.pageY - tipHeight - 20;
}
//Absolute position the tooltip according to mouse position
tip.css({ top: mousey, left: mousex });
}
});
}
$( window ).resize(function() {
if ($(window).width() < 963) {
tip = $(this).nextAll('#tail');
tip.hide();
}
else {
//Tooltips
$("#mainbody").hover(function(){
tip = $(this).nextAll('#tail');
tip.show(); //Show tooltip
}, function() {
tip.hide(); //Hide tooltip
}).mousemove(function(e) {
if ($(window).width() > 962) {
//Change these numbers to move the tooltip offset
var mousex = e.pageX + 20; //Get X coodrinates
var mousey = e.pageY + 20; //Get Y coordinates
var tipWidth = tip.width(); //Find width of tooltip
var tipHeight = tip.height(); //Find height of tooltip
//Distance of element from the right edge of viewport
var tipVisX = $(window).width() - (mousex + tipWidth);
//Distance of element from the bottom of viewport
//var tipVisY = $(window).height() - (mousey + tipHeight);
var tipVisY = $("#mainbody").height() - (tipHeight);
if (tipVisX < 1) { //If tooltip exceeds the X coordinate of viewport
//mousex = e.pageX - tipWidth - 20;
} if (tipVisY < 1) { //If tooltip exceeds the Y coordinate of viewport
//mousey = e.pageY - tipHeight - 20;
}
//Absolute position the tooltip according to mouse position
tip.css({ top: mousey, left: mousex });
}
});
}
});
});
});
</script>



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Вы можете вставить код в функцию и вызывать эту функцию где хотите.
$( document ).ready(function() { //document ready
$( window ).on( "load", function() { //entire page has loaded
change(this)
$( window ).resize(function() {
change(this)
});
});
});
function change(_this){
if ($(window).width() < 963) {
tip = $(_this).nextAll('#tail');
tip.hide();
}
else {
//Tooltips
$("#mainbody").hover(function(){
tip = $(this).nextAll('#tail');
tip.show(); //Show tooltip
}, function() {
tip.hide(); //Hide tooltip
}).mousemove(function(e) {
if ($(window).width() > 962) {
//Change these numbers to move the tooltip offset
var mousex = e.pageX + 20; //Get X coodrinates
var mousey = e.pageY + 20; //Get Y coordinates
var tipWidth = tip.width(); //Find width of tooltip
var tipHeight = tip.height(); //Find height of tooltip
//Distance of element from the right edge of viewport
var tipVisX = $(window).width() - (mousex + tipWidth);
//Distance of element from the bottom of viewport
//var tipVisY = $(window).height() - (mousey + tipHeight);
var tipVisY = $("#mainbody").height() - (tipHeight);
if (tipVisX < 1) { //If tooltip exceeds the X coordinate of viewport
//mousex = e.pageX - tipWidth - 20;
} if (tipVisY < 1) { //If tooltip exceeds the Y coordinate of viewport
//mousey = e.pageY - tipHeight - 20;
}
//Absolute position the tooltip according to mouse position
tip.css({ top: mousey, left: mousex });
}
});
}
}
Альтернативой является запуск обработчика событий вручную с помощью .trigger('[eventname]'):
$( window ).on( "load", function() {
$( window ).resize(function() {
//do some stuff
}).trigger('resize');
});
Этот вариант предполагает, что других обработчиков изменения размера нет, что небезопасно.
Спасибо. Работает как шарм.